iOS 7 Uiimagepicker Preview Black Screen

iOS 7 UIImagePickerController has black preview

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.

iDevice camera shows black instead of preview

I had faced this issue in my app. Though I never found out the what the issue was, I rewrote my code to define a property of UIIMagePickerController type and initialize it once in the getter. Used this property to initialize the camera view :

getter:

-(UIImagePickerController *) imagePicker{
if(!_imagePicker){
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else{
_imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
}

}
return _imagePicker;
}

- (IBAction)addNewImage:(id)sender{
if (self.imagePicker)
{
[self presentViewController:self.imagePicker animated:YES completion:^{}];
}
}

For some reason this got rid of the issue with preview sometimes showing a black screen



Related Topics



Leave a reply



Submit