iOS 7 UIImagePickerController has black preview

2019-01-06 15:36发布

问题:

I have a UIImagePickerController being called with sourceType camera and 80% of the time I get a black preview. If I wait, let's say around 30 seconds, I get a good preview and it will be good for around 50% of the time, then it can break again.

The image in question is very similar to this. iDevice camera shows black instead of preview

Other people imply that GCD might be causing some issues with the camera, and that updates to the UI while the image picker is being loaded break it. I put a lock on every GCD block that calls the main thread for this purpose.

This is an example of an image that rotates to simulate an activity indicator.

-(void)animateLoadingImage {

if (isMainThreadBlocked) {
    return;
}

self.radians += M_PI_4 / 2;
[UIView beginAnimations:@"progress rotation" context:nil];
[UIView setAnimationDuration:.1];
self.loadingImageView.transform = CGAffineTransformMakeRotation(self.radians);
[UIView commitAnimations];


}

PS: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

This shows ALWAYS when I try to open the picker controller, but it shows it even if the camera shows the preview correctly. I don't think the error is around here but this bugs me a lot also.

回答1:

There doesn't seem to be a good answer to this anywhere online, so I had to slog through this myself to figure it out.

I'm using ARC (like probably most others these days), so the answer above didn't really help me.

This black camera issue happens as a side effect of doing UIKit stuff off of the main thread on iOS 7. Broadly, background UIKit operations results in all sorts of weird things happening on iOS 7 (memory not being deallocated promptly, weird performance hitches, and the black camera issue).

To prevent this, you need to not do ANY UIKit stuff on background threads.

Things you cannot do on background threads: - Allocate/initialize UIViews and subclasses - Modify UIViews and subclasses (e.g., setting frame, setting .image/.text attributes, etc).

Now, the problem with doing this stuff on the main thread is that it can mess with UI performance, which is why you see all these iOS 6 "background loading" solutions that DO NOT work optimally on iOS 7 (i.e., causing the black camera issue, among other things).

There are two things that I did to fix performance while preventing the ill effects of background UIKit operations: - Pre-create and initialize all UIImages on a background thread. The only thing you should be doing with UIImages on the main thread is setting "imageView.image = preloadedImageObject;" - Re-use views whenever possible. Doing [[UIView alloc] init] on the main thread can still be problematic if you're doing it in a cellForRowAtIndex or in another situation where responsiveness is super important.

Best of luck! You can test how you're doing by monitoring memory usage while your app is running. Background UIKit => rapidly escalating memory usage.



回答2:

During one of my recent projects I had this same (or similar) issue, namely the Camera preview showing a black screen when using UIImagePickerController on iOS7 (iPad).

What I discovered is that GCD in itself is not the problem. The problem occurs when you try to use anything at all from the UIKit framework in a thread other than the main thread (as part of an NSOperation).

First of all, let me start by saying that it is clear to me that you shouldn't do this to begin with. In my specific case, however, I had no choice. Since I think that there's a good chance that other people might be experiencing the same weird UIImagePickerController camera behaviour with the same cause, I made up some very simple example code explaining what's going on.

You can find it here: https://github.com/idamediafoundry/CameraTest

The ViewController shows 2 simple buttons. One calls UIImagePickerController, showing the camera, the other starts some GCD operations doing a simple task. If you just open the camera, everything works fine. If you first start the operations and then open the camera, the black preview issue occurs.

Again, you shouldn't call UIKit in anything but the main thread. But I don't think it's normal that breaking this rule causes the camera to malfunction when opening it later in the main thread as part of a completely different flow, right?

Hope this helps.



回答3:

I had this same issue, but the app would never show the alert to grant permission to access the photos, and it would never show up in Settings->Privacy->Photos as having asked for permission. It would simply display the black screen without a permission prompt.

I finally traced this to the info.plist.

As it turns out if you add the key "Bundle display name" to the info.plist and leave the value blank the permission alert will never display. It seems the permissions alert pulls this value to display in the alert, and if you leave it blank it simply will not show the alert.

If you have this in your plist without a value it could be causing a black preview.

EDIT: I was experiencing this issue with some devices and not others, with my testing all 3 of the iPhone 6 devices I tested with and the iPhone 5s would not show the alert and would hang on the black screen. An iPhone 4s and iPhone 6S+ showed the alert and worked as expected.



回答4:

Firstly, this issue does seem partly Apples fault and the developers, probably more on developers side. I was able to get my app to produce a black preview image by toggling between native camera app and my own app's usage of UIImagePickerController camera mode. It was not consistent but I did make it occur doing those steps repeatedly for about 5-15 times, eventually it would become black preview.

On the other hand, I was able to address this issue by refactoring my own code. I didn't need to build a custom camera like others may have done with AVFoundation, rather, I removed all potential / valid memory leaks from my code. Make sure you test your app with Instruments, get rid of any leaks. Clean up UIImagePickerController also, that may differ depending on using ARC or not. After my refactor, the problem with black preview went away. I believe a memory leak in your app is what is causing the video preview to become black after sometime.

Last but not least, I had another app which uses zxing scanner, which uses AVFoundation for decoding images from live preview stream...this one was very tricky to fix, it was ARC mixed with non-arc( zxing non-arc). I had to refactor a bunch more code as the view / UIViewController hierarchy was much more complex and getting the actual object which contains the AVFoundation Session to dealloc was the key. After a day of refactoring this problem went away, so yes, plugging memory leaks was the answer in both my cases of the black image preview problem in iOS 7.



回答5:

In my case I had to move some methods to the main thread.

My app creates a new Image with a new context, I can create the new context in a different thread and use the CGContext functions (like CGContextScaleCTM or CGContextTranslateCTM or CGContextConcatCTM) and [uiimage drawInRect:Mybounds];.

Basically I had to move to the main thread the renderInContext method when I drew layer in the context:

CGSize sizeView = viewPrintBase.frame.size;
UIGraphicsBeginImageContext(sizeView);
currentContext = UIGraphicsGetCurrentContext();

dispatch_sync(dispatch_get_main_queue(), ^{
    [viewPrintBase.layer renderInContext:currentContext];
    [imageViewPhotoUser.layer renderInContext:currentContext];
    if (imageViewMask) {
        [imageViewMask.layer renderInContext:currentContext];
    }
});
[imageSnapShotDrawinfView drawInRect:viewPrintBase.bounds];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([finalImage CGImage], viewPrintBase.bounds);
// or use the UIImage wherever you like
imageSnapShot = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

AlsoI had to move to the main thread all UI objects creation, like:

    __block UIView *viewPrintBase;
dispatch_sync(dispatch_get_main_queue(), ^{
    viewPrintBase = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_FINAL_IMAGE, HEIGHT_FINAL_IMAGE)];
    viewPrintBase.backgroundColor = [UIColor whiteColor];
    [viewPrintBase setClipsToBounds:YES];
    //We get the photo user
});

I hope this helps :)



回答6:

first of all check whether your app has the permission to access the camera or not.I think you pressed "no" option for the first time the camera launches in the app.Now do one thing. go to Settings->Privacy->Camera then choose your app there check whether its on of off if its off make it on..now you can access the camera in the app.



回答7:

Seems like this is iOS 7 issue. Even I faced a similar situation where when I upload the photo from the image gallery from within my app, and then try opening camera black screen appears.