开发者

UIImagePickerController in custom view doesn't show camera

开发者 https://www.devze.com 2023-02-19 22:04 出处:网络
I\'m trying to display the content of the camera in a custom view. What I just want to achieve is to have custom buttons to take pictures in order to take more than one photo at a single time.

I'm trying to display the content of the camera in a custom view. What I just want to achieve is to have custom buttons to take pictures in order to take more than one photo at a single time.

It should work out of the box, theoretically, but in practice sometimes it happens that if I dismiss my custom view controller and then I 开发者_开发问答re-open it "quickly", the UIImagePickerController just shows a blank (black, actually) content. The funny thing is that if you try to take a picture, the camera actually is enabled and the shutter opens and you can collect the image. The only issue seems to be related to displaying the live-content into a specific UIView.

This is the code I use for displaying it:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePickerController setShowsCameraControls:NO];
[imagePickerController setEditing:NO];
[imagePickerController setNavigationBarHidden:YES];

[imagePickerView addSubview:[imagePickerController view]];
[imagePickerController viewWillAppear:YES];

I don't like that viewWillAppear method call but it is the only way I found in order to show it. imagePickerView is, indeed, the view that I have previously created to place the picker into.

By digging a little bit the problem myself, I noticed that if I wait a couple of seconds before re-opening my custom view controller, the picker shows up normally.

By taking a look into the console it seems that the picker (or the camera resource associated to it) is actually released after a while but this is just a guess.

Any clue? Thanks


You definitely need that viewWillAppear call, and likely more. Whenever a UIImagePickerController is presented by one of the "indirect" presentation methods (like a modal presentation, or being pushed on a navigation stack), it's automatically sent all of the appropriate display related notifications: viewWillAppear:, viewDidAppear:, viewWillDisappear:, and viewDidDisappear:.

Internally UIImagePickerController uses these notifications to take appropriate initialization actions, like the shutter effect. You don't know how it uses them, you just have to be sure it gets them.

When you present the UIImagePickerController directly by adding it's view as a subview, you deprive it of automatically receiving these notifications. From the View Controller Programming Guide:

If you incorporate a view controller’s view into your hierarchy by other means (by adding it as a subview to some other view perhaps), the system assumes you want to manage the view yourself and does not send messages to the associated view controller object.

This isn't necessarily bad, it just means you need to shoulder the responsibility for those messages yourself. I haven't seen this exact issue that you're having with display of the picker, but my first attempt at a fix would be to ensure that each of those 4 display related notifications are sent to the picker controller at the appropriate time, especially the disappearing ones, if you aren't already doing so.

0

精彩评论

暂无评论...
验证码 换一张
取 消