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
Swift: How to Refresh Uicollectionview Layout After Rotation of the Device
Converting Utc Date Format to Local Nsdate
Cropping Image with Swift and Put It on Center Position
The Maximum Number of Apps for Free Development Profiles Has Been Reached. Xcode 11.5
Upload Image to the PHP Server from iOS
Uploading Archive Error: "Missing iOS Distribution Signing Identity for ..."
Convert Uiimage to Nsdata and Convert Back to Uiimage in Swift
Nsinteger and Nsuinteger in a Mixed 64Bit/32Bit Environment
Navigating to a New Screen When Stream Value in Bloc Changes
What Size Should Tabbar Images Be
Iphone:How to Detect the End of Slider Drag
iOS 11 Uitabbar Uitabbaritem Positioning Issue
Ios7 Status Bar Hide/Show on Select Controllers
Xcode 10: a Valid Provisioning Profile for This Executable Was Not Found
iOS Designated Initializers:Using Ns_Designated_Initializer
Issue with Unsafepointer<Uint8> in SQLite Project in Swift
Return from Initializer Without Initializing All Stored Properties