I'm getting a black image picker preview. This DOES NOT happen if I delete the app then run it again. So the first time the app runs after being installed to my device it works. However if I bring up my image picker controller after that and capture an image, the preview is black. I have read all the answers mentioning background threading but I am not running any background threads. Im not sure what is causing this. It works perfectly the first time around but after that it never works.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Image Picker settings
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//dismiss view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Don't present the image picker from the viewDidLoad method of your view controller. When viewDidLoad is being run, your view controller hasn't yet been added to the view controller hierarchy. You can try presenting it from viewDidAppear, and that might fix it.
Related
This application loads the device's camera in viewWillAppear method:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (self.imageView.image == nil) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
else { }
}
Delegate methods are implemented:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[self dismissViewControllerAnimated:YES completion:nil];
// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:#selector(composeEmail) withObject:image afterDelay:1.0];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}
When a photo is taken and I choose the default "Use Photo" button I want to dismiss the camera ViewController and load the emailComposer View Controller, which uses this method:
- (void) composeEmail: (UIImage *)image {
NSString *bodyHeader = #"Here are you directions:";
NSString *mailBody = [NSString stringWithFormat:#"%#\n%#", bodyHeader, googleMapsURL];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Google Maps Directions"];
[picker setMessageBody:mailBody isHTML:NO];
[picker setToRecipients:#[#"john.doe#gmail.com"]];
[picker setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation(image);
// Attach image data to the email
// 'DestinationImage.png' is file name that will be attached to the email
[picker addAttachmentData:data mimeType:#"image/png" fileName:#"DestinationImage"];
[self presentViewController:picker animated:YES completion:nil];
}
(I have left out some of the MessageUI details here but I have tested it and I know it is working)
The image taken should be passed to the emailComposer and attached as an email. When I build this on my device and tap the "Use Photo" button an error is thrown. The error message states "Attempt to present MFMailCompseViewController on ViewController whose view is not in the window hierarchy!" I am using only one ViewController and that VC contains one Image View.
Can anyone help me dismiss the camera and load the email composer?
Much appreciated!
First Dismiss the ImagePicker and then present ur mailcomposer will work then.U r dismissing the parent thta's y its not working [yourpicker dismissViewControllerAnimated:YES completion:nil];
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[yourpicker dismissViewControllerAnimated:YES completion:nil];
// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:#selector(composeEmail) withObject:image afterDelay:1.0];
}
To present view controller, use following :
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController: picker
animated:YES
completion:nil];
You can also look into this stackoverflow's question for further understanding
I am taking picture from Camera.They Shown me 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.
-(IBAction)choosePicture:(id)sender
{
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate=self;
[self presentViewController:imagePicker animated:YES completion:nil];
}
-(IBAction)takePicture:(id)sender
{
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
// UIImage *pickedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
image.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
picker.modalPresentationStyle = UIModalPresentationFullScreen;
[picker dismissViewControllerAnimated:YES completion:nil];
}
Tried your code in an empty app, had no messages, everything is fine.
Besides downloaded example from apple https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html
It has the same problem, but everything works fine. May be it's just a bug. (If their own code has the same message)
If you have this message and something doesn't work correctly, try similar questions. for example: UIImagePickerController error: Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7
This worked for me:
-(IBAction)takePicture:(id)sender
{
UIImagePickerController *imagePickController=[[UIImagePickerController alloc]init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else
{
imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePickController.delegate = self;
imagePickController.allowsEditing = TRUE;
[self presentViewController:imagePickController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
<your image> = [info objectForKey:UIImagePickerControllerEditedImage];
}
Ive been trying to set up a small iOS method that takes a picture automatically when user opens the app. After much research i finally found this iOS taking photo programmatically and after a little more i found this from apple https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html can someone help me get started in setting up a method for captureStillImageAsynchronouslyFromConnection:completionHandler:
i don't want any interaction from said user. thanks
Even though you seem to want to do the call asynchronously and using AVFoundation, I still recommend simply using a UIImagePickerController in this case, ex:
- (void)viewDidLoad {
[super viewDidLoad];
// If the device has a camera...
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Create an image picker
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imagePickerController.showsCameraControls = NO;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
// And take the picture after a short delay
// to give the view and image picker time to get
// ready
[self performSelector:#selector(takepic:) withObject:imagePickerController afterDelay:2];
}];
}
}
// Automatically take the picture using the
// image picker passed in as a parameter
- (void)takepic:(UIImagePickerController*)imagePickerController {
[imagePickerController takePicture];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// ... Do whatever with the image ...
[picker dismissViewControllerAnimated:YES completion:nil];
}
I need to display the image picker when the user enters into ViewControllerA and stay there after the user made a photo and pressed "Use Photo" button.
Actually the problem comes out when the user tap the "Use Photo" button, in this case the image picker pops up again time after time. I know it's because the viewWillAppear, but the viewDidLoad also not a good choice, because in this case the image picker won't be appear more than 1 time.
Possibly somebody could give me some guidance that how can I close the image picker without this issue? I just want to display it everytime the user enters in the view controller and close it after the user has choosen an image.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *choosenImage = info[UIImagePickerControllerEditedImage];
self.placeholderImg.image = choosenImage;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:NO completion:nil];
[self.tabBarController setSelectedIndex:1];
}
In your view controller's .m file you can declare a BOOL that will keep up if you've presented the UIImagePickerController already.
#interface MyCustomViewController ()
{
BOOL imagePickerHasBeenPresented;
}
#end
And then in your viewWillAppear method check that bool:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (imagePickerHasBeenPresented == NO)
{
imagePickerHasBeenPresented = YES;
//Code to present imagePicker
}
}
When I am using UIImagePickerController in IOS7.0.3 it seems to throw an error "Snapshotting a view that has not been rendered results in an empty snapshot". My code is shown below
-(void) showCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
Delegate method is
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES
completion:nil];
NSString *mediaType = info[UIImagePickerControllerMediaType];
UIImage *image = nil;
if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage])
{
image = info [
UIImagePickerControllerOriginalImage];
}
if(picletImage != nil)
{
self.imageView.image = image;
}
}
I dont know whats wrong here but this seems to be working fine on iPad. I read through good number of articles but did not get a solution for this . I verified in instruments and seems to be a memory leak happening during camera initialization or once the picture is captured. I have been stuck with this for past 4 days , can some one can help me out as what is going wrong and also a possible solution for this.