iOS 7 UIImagePickerController has black preview

后端 未结 7 1723
星月不相逢
星月不相逢 2020-11-29 04:29

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

相关标签:
7条回答
  • 2020-11-29 05:07

    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.

    0 讨论(0)
  • 2020-11-29 05:09

    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.

    0 讨论(0)
  • 2020-11-29 05:11

    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.

    0 讨论(0)
  • 2020-11-29 05:20

    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 :)

    0 讨论(0)
  • 2020-11-29 05:20

    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.

    0 讨论(0)
  • 2020-11-29 05:22

    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.

    0 讨论(0)
提交回复
热议问题