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.
Related
I am getting image from a photo album with this code:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];}
Please Could anyone help me to assign this image to a variable? I want to blur it later using stackblur:
UIImage *newIma=[oldIma stackBlur:radius];
Thanks in advance!
Set the UIImage when you're getting the image that the user has selected.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
// Create the UIImage in your #implementation and set it here
myImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
myImageView.image = myImage;
}
How do I use the new Photo Framework to get a UIImage from a given image URL which the UIImagePicker returns?
I hope I understood you right...
You can open an UIImagePickerController like this:
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
And after picking you get the UIImage from the pickers delegate like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
The chosenImage will be your Image
EDITED after complain
Im sorry dude... the Key I have chosen is only available if you set picker.allowsEditing = true; property on true...
So if you don't need the editing after you have chosen your image, use this one
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
instead of
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
I have just tried it, it will work...
My code
- (IBAction)takePhoto:(UIButton *)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *picker = [UIImagePickerController new];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR!" message:#"Device has no camera!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
and the delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.userImage.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Well, thats pretty simple and basic. My problem is that after assigning my image to the UIImageView I have (which has a pinned/fixed width and height) instead of resizing itself to the size of the uiimageview it stretches throughout my whole view, working more as a background that an image placed where it should be. Again, the width and height of my uiimageview has been pinned. Can anybody help? thanks
The contentMode of your UIImageView should be UIViewContentModeScaleAspectFit, instead of UIViewContentModeCenter as it seems to be.
If you need to change the content mode on the fly, just add this line:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.userImage.image = chosenImage;
// Add this line
self.userImage.contentMode = UIViewContentModeScaleAspectFit;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I've got some basic code to let the user pick an image and set it to an already created UIImageView:
- (void)addAvatarImageView {
self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 83, 83)];
self.avatarImageView.imageURL = [[NSURL alloc] initWithString:self.user[#"avatar"]];
[self.avatarView addSubview:self.avatarImageView];
}
and the picker creation:
- (void)showAvatarPicker {
UIImagePickerController *imagePickController = [[UIImagePickerController alloc] init];
imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickController.delegate = self;
imagePickController.allowsEditing = YES;
[self.navigationController presentViewController:imagePickController animated:YES completion:nil];
}
and the picker delegate:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.avatarImageView setImage:image];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
I can breakpoint inside the delegate method and it goes in there fine, but the UIImageView does not show the new image at all. I can't even remove the image that is present either. Any ideas?
I am going to go on a limb here and say that the error is in the portion of the code that you are not showing: where you instantiate your picker. More presicely, you may not have set allowsEditing to yes. So take a look below
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
Alternatively you can just read the original image using UIImagePickerControllerOriginalImage instead of your current UIImagePickerControllerEditedImage.
change UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; to UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
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];