In iOS 8 I am having problem capturing images from camera till now I am using this code for
UIImagePickerController *controller=[[UIImagePickerController alloc] init];
controller.videoQuality=UIImagePickerControllerQualityTypeMedium;
controller.delegate=(id)self;
controller.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:controller animated:YES completion:nil];
But in iOS 8 I am getting this:
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.
I have tried with the solution provided by This Post with
#property (strong,nonatomic)UIImagePickerController *controller;
_controller=[[UIImagePickerController alloc] init];
_controller.videoQuality=UIImagePickerControllerQualityTypeMedium;
_controller.delegate=(id)self;
_controller.sourceType=UIImagePickerControllerSourceTypeCamera;
_[self presentViewController:controller animated:YES completion:nil];
and this
...
controller.modalPresentationStyle=UIModalPresentationFullScreen;
or
controller.modalPresentationStyle=UIModalPresentationCurrentContext;
...
and this
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self presentViewController:controller animated:YES completion:nil];
});
and this
[self presentViewController:controller animated:YES completion:NULL];
and this
[self presentViewController:controller animated:YES completion:^{
}];
any idea?
I'm pretty sure this is just a bug in iOS 8.0. It's reproducible with the simplest of POC apps that does nothing more than attempt to present a UIImagePickerController like you're doing above. Furthermore, there's no alternative pattern to displaying the image picker/camera, to my knowledge. You can even download Apple's Using UIImagePickerController sample app, run it, and it will generate the same error out of the box.
That said, the functionality still works for me. Other than the warning/error, do you have issues with the functioning of your app?
I was struggling with this issue for several hours, i have read every relevant topic and found out that the error was caused because under the privacy settings of my device, the camera access to my app was blocked!!! I have never denied access to camera and i don't know how it was blocked but that was the problem!
I don't have enough reputation points to comment on #greg's answer above, so will add my observations here. I have a Swift project for both iPad and iPhone. I have a method inside my main view controller (relevant bit below). When I test this on a phone, everything works properly and no warnings are generated. When I run it on an iPad, everything works properly but I see the warning about snapshotting the view. The interesting bit, however, is that when I run on an iPad without using the popover controller, everything works properly with no warning. Unfortunately, Apple mandates that the image picker must be used within a popover on iPad, if the camera is not being used.
dispatch_async(dispatch_get_main_queue(), {
let imagePicker: UIImagePickerController = UIImagePickerController();
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
imagePicker.mediaTypes = [kUTTypeImage];
imagePicker.allowsEditing = false;
imagePicker.delegate = self;
if(UIDevice.currentDevice().userInterfaceIdiom == .Pad){ // on a tablet, the image picker is supposed to be in a popover
let popRect: CGRect = buttonRect;
let popover: UIPopoverController = UIPopoverController(contentViewController: imagePicker);
popover.presentPopoverFromRect(popRect, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Up, animated: true);
}else{
self.presentViewController(imagePicker, animated: true, completion: nil);
}
});
I ran into this after calling UIImagePickerController presentViewController: from the callback to a UIAlertView delegate. I solved the issue by pushing the presentViewController: call off the current execution trace using dispatch_async.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
if (buttonIndex == 1)
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
else
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController: imagePickerController
animated: YES
completion: nil];
});
}
I had this issue when animating some views and the app would go into background mode and come back. I handled it by setting a flag isActive. I set it to NO in
- (void)applicationWillResignActive:(UIApplication *)application
and YES in
- (void)applicationDidBecomeActive:(UIApplication *)application
and animate or not animate my views accordingly. Took care of the issue.
I had this with an UIAlertControllerStyleActionSheet giving the user the option to take a photo with the camera or use one from library.
I tried a symbolic breakpoint on the error message
That showed me the error is produced by the intern use of a UICollectionView during presentation
[self presentViewController:alert animated:YES completion:nil];
I fixed this by explixitly setting the frame before presenting
[alert setPreferredContentSize: alert.view.frame.size];
Here is the complete methode that is working without the error
-(void)showImageSourceAlertFromSender:(id)sender{
UIButton *senderButton = (UIButton*)sender;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:#"Camera" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self takePhoto];
}];
UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:#"Library" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self selectPhotoFromLibraryFromSender:sender];
}];
[alert addAction:cameraAction];
[alert addAction:libraryAction];
alert.popoverPresentationController.delegate = self;
alert.popoverPresentationController.sourceRect = senderButton.frame;
alert.popoverPresentationController.sourceView = self.view;
[alert setPreferredContentSize: alert.view.frame.size];
[self presentViewController:alert animated:YES completion:^(){
}];}
You can silence the "Snapshotting a view" warning by referencing the view property before presenting the view controller. Doing so causes the view to load and allows iOS render it before taking the snapshot.
UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.popoverPresentationController.barButtonItem = (UIBarButtonItem *)sender;
... setup the UIAlertController ...
[controller view]; // <--- Add to silence the warning.
[self presentViewController:controller animated:YES completion:nil];
For anyone that is seeing an issue with a black preview after image capture, hiding the status bar after the UIPickerController is shown seems to fix the issue.
UIImagePickerControllerSourceType source = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.delegate = self;
cameraController.sourceType = source;
cameraController.allowsEditing = YES;
[self presentViewController:cameraController animated:YES completion:^{
//iOS 8 bug. the status bar will sometimes not be hidden after the camera is displayed, which causes the preview after an image is captured to be black
if (source == UIImagePickerControllerSourceTypeCamera) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
}
}];
I found the same issue and tried everything. I have two different apps, one in objective-C and one in swift - both have the same problem. The error message comes in the debugger and the screen goes black after the first photo. This only happens in iOS >= 8.0, obviously it is a bug.
I found a difficult workaround. Shut off the camera controls with imagePicker.showsCameraControls = false and create your own overlayView that has the missing buttons. There are various tutorials around how to do this.
The strange error message stays, but at least the screen doesn't go black and you have a working app.
This might be a bug of built-in ImagePickerController. My code is working, but occasionally crashes on iPhone 6 Plus.
I've tried all solutions suggested by other answers but there were no luck. Problem finally solved after switching to JPSImagePickerController.
I've tried everything, my problem was that the image picker for the camera and photo library disappeared right after they showed. I solved it with the following line (swift)
imagePicker.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
I'm pretty sure this is just a bug in iOS 8.0. It's reproducible with the simplest of POC apps that does nothing more than attempt to present a UIImagePickerController like you're doing above. Furthermore, there's no alternative pattern to displaying the image picker/camera, to my knowledge. You can even download Apple's Using UIImagePickerController sample app, run it, and it will generate the same error out of the box.
That said, the functionality still works for me. Other than the warning/error, do you have issues with the functioning of your app?
If we are using the UIImagePickerController as a property, then this warning will disappear. xcode assume that we are not using the result from the UIImagePickerController , if we are instantiating the UIImagePickerController within a function.
Calling this method worked for me. Place it after presenting your view.
[yourViewBeingPresented.view layoutIfNeeded];
I also encounter the same problem and I resolved it by checking if the camera is available:
BOOL cameraAvailableFlag = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if (cameraAvailableFlag)
[self performSelector:#selector(showcamera) withObject:nil afterDelay:0.3];
I have came across with this issue. When we call the camera and release the views produced this issue. For an example call an camera and set view nil in viewDidDisappear method this error will come since there is not callback for camera event. Make sure about this case too for this error.
I got the same bug,getting bellow message in console while opening camera.
'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.'
For me problem was with the Bundle display name in Info.plist file.it was empty some how,i put my app name there and now it working fine.i did't received any camera permission alert because of empty Bundle display name.it blocked the view from rendering.
the problem was't with view but by presenting it without a permission.you can check it on settings-->privacy-->Camera,if your app not listed there problem might be same.
I'm using Phonegap, but this thread keeps coming as the first one when Googling about the error message.
For me this issue went away by defining the imagetype to PNG.
encodingType : Camera.EncodingType.PNG
So the whole line being:
navigator.camera.getPicture(successFunction, failFunction, { encodingType : Camera.EncodingType.PNG, correctOrientation:true, sourceType : Camera.PictureSourceType .PHOTOLIBRARY, quality: 70, allowEdit : false , destinationType: Camera.DestinationType.DATA_URL});
Your mileage may vary, but that did the trick for me.
Alternatively, consider using drawViewHierarchyInRect:
Swift:
extension UIImage{
class func renderUIViewToImage(viewToBeRendered: UIView) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(viewToBeRendered.bounds.size, true, 0.0)
viewToBeRendered.drawViewHierarchyInRect(viewToBeRendered.bounds, afterScreenUpdates: true)
viewToBeRendered.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
}
Objective-C:
- (UIImage *)snapshot:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Also see:
https://developer.apple.com/library/ios/qa/qa1817/_index.html
How to capture UIView to UIImage without loss of quality on retina display
In my case ( XCode 7 and iOS 9 ), I use UINavigationController "hidden", so Ihave to add UINavigationControllerDelegate to present camera or roll and it work like it is supposed to! And pickerControllerDelegate.self doesn't display error either!
Iam using the following code for taking the picture automatically from IPAD front camera:
UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPkr.delegate = self;
imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;
[self presentModalViewController:imgPkr animated:YES];
imgPkr.showsCameraControls = NO;
[imgPkr takePicture];
But this code Dont take any picture and dont call the delegate:
-(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info
Any idea what is wrong with the code
My first guess is that [imgPkr takePicture]; is being called before the picker is done being presented. Try it like this:
UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPkr.delegate = self;
imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;
[self presentModalViewController:imgPkr animated:YES];
imgPkr.showsCameraControls = NO;
[self performSelector:#selector(takePicture) withObject:self afterDelay:1.0];
OR
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(takePicture)
name:AVCaptureSessionDidStartRunningNotification
object:nil];
&
- (void)takePicture
{
[imgPkr takePicture];
}
I bet that you get a message in your logs saying that
UIImagePickerController: ignoring request to take picture; camera is
not yet ready.
This is a common problem because the underlying capture session needs some time to start, so you just have to be sure that the camera is ready to take a picture and then call takePicture method. Now, a way to get notified is explained in detail in my answer here: How to know if iPhone camera is ready to take picture?
Note though that this method will work on iOS5+ (there is a bug in older versions that prevents the system notifications for this event, contrary to what is described in the documentation). I hope that this helps.
another way to wait for camera ready until take picture is completion block ->
[self presentViewController:imagePicker animated:YES
completion:^ {
[imagePicker takePicture];
}];
thank you 'GrandSteph' at
iOS taking photo programmatically
I have an app that uses the UITabBarController and one of the tabs is supposed to be a full on camera view (similar to Instagram). When I launch the app and go to that view, most of the screen is blank and no image library picker/camera view loads. Any help would be greatly appreciated.
Here is my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[imagePicker setEditing:NO];
// Place image picker (camera viewing angle on the screen)
[self presentViewController:imagePicker animated:YES completion:nil];
}
Try putting that code in viewDidAppear instead -- that worked for me. Better yet, would be to put all but the last line in viewDidLoad, so it's only called once, and just put the last line in the viewDidAppear method. When you cancel the image picker (the only thing I tested), it will just reappear, since viewDidAppear will be called again, so you need to implement imagePickerControllerDidCancel: like this to keep that from happening:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
self.imagePicker = nil;
}
And, in your viewDidAppear, put in an if statement to see if the picker exists before trying to present it:
if (self.imagePicker) {
[self presentViewController:self.imagePicker animated:YES completion:nil];
}