issues with "imagePickerController: didFinishPickingMediaWithInfo:" - ios

I keep getting an unused variable warning. I'm trying to have a modal view appear, where a user selects a photo, and I want to use that selected image in an UIImageView.
Not sure if this is enough info but would really appreciate any help.
- (IBAction) buttonPressed:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void) imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
}

you're getting the warning because you're not using the image object anymore. You must save this image in some class object to use it somewhere in the code, warning will automatically vanish if you do that. Or just for removing the warning you can do:
-(void) imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
UIImage *image;
image=[info objectForKey: UIImagePickerControllerOriginalImage];
}

Related

UIImagePickerController + Cropping: what does allowEditing and what does it not?

After reading the documentation and sample code from apple regarding the UIImagePickerController and after the read of many tutorials and even Questions here at Stackoverflow (or here), i came to the conclusion, that the builtin UIImagePicker is able to present a "crop window" to the User and let the user crop and move the right part of the Image he wants to use.
But checking out these examples on iOS 7.1, there appears no cropping window. There appears nothing what let the user crop out a part of the taken image. All tests occured on real hardware (iPhone 5S), no iPhone simulator was used here because of missing camera ;)
Also, at the delegate method "didFinishPickingMediaWithInfo" there is no entry of "UIImagePickerControllerEditedImage" in the "info"-NSDictionary.
What i did:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
#if TARGET_IPHONE_SIMULATOR
// iPhone-Simulator has no camera, just save images to the photoalbum on the simulator to use them here
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
imagePickerController.editing = YES;
imagePickerController.delegate = (id)self;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[self presentModalViewController:imagePickerController animated:YES];
and the delegate method:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// .... do something
}
At this point, only 3 Keys are available in the Dictionary with the name "info":
UIImagePickerControllerOriginalImage
UIImagePickerControllerMediaMetadata
UIImagePickerControllerMediaType
nothing else. Also, no cropping window or any similar controls appeared.
Am i wrong with my expectation about the behaviour of the ImagePicker? At this SF-Question the user wrote "Crop window comes up". Was the feature removed? Is the documentation outdated or am i completely wrong here?
I also tried this code taken from another answer to a similar question:
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
// Edited image works great (if you allowed editing)
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
// AND the original image works great
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND do whatever you want with it, (NSDictionary *)info is fine now
UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
}];
}
No cropping window appears, the key "UIImagePickerControllerEditedImage" is not present at the info-NSDictionary.
Can someone explain me the right behaviour? Thank you very much in advance.
this one is works for me:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage, *editedImage;
editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
if (editedImage)
self.profilePictureImageView.image = editedImage;
else
self.profilePictureImageView.image = originalImage;
[picker dismissViewControllerAnimated:YES completion:nil];
}
Firstly make sure you import the following header file (you must import the MobileCoreServices framework to your project):
#import <MobileCoreServices/UTCoreTypes.h>
Then try the following code
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
#if TARGET_IPHONE_SIMULATOR
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
#else
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
#endif
imagePicker.mediaTypes = #[(NSString *)kUTTypeImage];
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
//[self presentModalViewController:imagePicker animated:YES]; <-- This method is deprecated use the following instead
[self presentViewController:imagePicker animated:YES completion:nil];

How can I retrieve the image taken using UIImagePickerControllerDelegate with ARC enabled?

I have an app that needs to take a picture and then show it to the user. I opened the camera to let the user take a picture using the following code:
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Camera failed to open"
message: #"Camera is not available"
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
This present the camera view so the user can take a picture. I implemented the methods to handle the cancel and complete events like this:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
[photoView setImage:image];
[photoView setNeedsDisplay];
}
The problem is that after going through the last function, image is nil. At first dismissViewControllerAnimated was at the end of the function, but in this SO post the answer explains that dismissViewControllerAnimated should be called first and picker should be released before getting the image from info. I tried putting dismissViewControllerAnimated at the top, but because I'm using ARC I cannot release picker.
How can I get the image from info?
You have set picker.allowsEditing = NO; in your code. Then why are you retrieving the edited image? It won't exist. Anyways, if you allow editing, use this:
photoView.image = info[UIImagePickerControllerEditedImage] ? info[UIImagePickerControllerEditedImage] : info[UIImagePickerControllerOriginalImage]
If the user has edited the photo, photoView.image will be set to the edited photo, otherwise it will be set to the original photo.
If you don't allow editing, simply retrieve the image using the UIImagePickerControllerOriginalImage key.
try this:
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// or UIImage *image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
Check in this method,
(void) imagePickerController:(UIImagePickerController *)_picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"dictionary image info: %#",info );
}
In this method, print the dictionary and check that are you getting UIImagePickerControllerMediaType and UIImagePickerControllerOriginalImage.
(In my case, I m getting value like this UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = UIImage: 0x1dd79520";)
Now if you are getting value for UIImagePickerControllerOriginalImage then you save value in this UIImage.
Now use this.
UIImage *image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
OR
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
Hope this will help you.

UIImagePickerController in IOS7 Snapshotting a view that has not been rendered results in an empty snapshot

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.

pushViewController in the method didFinishPickingMediaWithInfo

When i use pushViewController i get to a blank view with a navigation button- Saved photos, and i don't know why.
I want to move to ReportViewController after selecting an image.
this is my code and it seems ok. what can possibly went wrong?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//Geting the image from the camera
image = [info objectForKey:UIImagePickerControllerOriginalImage];
ReportViewController *imageSelectionVC = [[ReportViewController alloc] init];
[imageSelectionVC.imageReport setImage:image];
[picker pushViewController:imageSelectionVC animated:YES];
}
Just as the comments say, you have to dismiss the picker and push the new controller to your animation controller (assuming you have one)
This is the correct code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
ReportViewController *imageSelectionVC = [[ReportViewController alloc] init];
[imageSelectionVC.imageReport setImage:image];
[self.navigationController pushViewController:imageSelectionVC animated:YES];
}];
}

ios how dismiss UIImagePickerController after use

I develop an app that take a picture from camera unitl here all well bat after receive from his delegate i dismiss and i receive exc_bad_access here is the code:
declare and open the camera:
-(void)ScattaFoto{
UIImagePickerController *picke = [[UIImagePickerController alloc] init];
// Delegate is self
picke.delegate = self;
// Allow editing of image ?
picke.allowsEditing=NO;
if(TARGET_IPHONE_SIMULATOR) {
picke.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
picke.sourceType = UIImagePickerControllerSourceTypeCamera;
}
// Show image picker
[self presentModalViewController:picke animated:YES];
}
and than is the delegate event:
- (void) imagePickerController:(UIImagePickerController *)picke didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
//UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// [picker release];
[self dismissModalViewControllerAnimated: YES]; // Here i receive exc_bad_access
[picke release];
}
i put on .h the refer to delegate of imagePickerControl "UINavigationControllerDelegate, UIImagePickerControllerDelegate"
Someone can help me?
- (void) imagePickerController:(UIImagePickerController *)picke didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picke dismissModalViewControllerAnimated: YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
Maybe ARC is releasing your UIImagePickerController before didFinishPickingMediaWithInfo is called?
Try placing your UIImagePickerController in a #property like this:
#property (nonatomic, strong) UIImagePickerController *picke;
You can read more about #properties here.
You will still have to allocate and initialize the UIImagePickerController before you use it.

Resources