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

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];

Related

handling captureStillImageAsynchronouslyFromConnection:completionHandler:

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];
}

How to load a UIImage from PhotoStream with UIImagePicker and Photo Framework under iOS 8.1?

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...

Upload image from UIImagePicker to server via php

I have an UIImageView which load profile picture of the user. I add some UIImagePicker features and now the user able to change the profile picture either by using camera or add from library. This is the code:
- (IBAction)cameraAction:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)libraryAction:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
_profilePhoto.image = chosenImage;
UIImageWriteToSavedPhotosAlbum(_profilePhoto.image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Now then, I had php files to upload the current image to server. The problem is, the input parameter for the php is image file name. I do looking around for quite some times and known that to get the image name, first I need to save it to apps document directory and rename it with the name I want later. So can anyone tell me how to save this image to apps document directory, how to get it and how to rename it with the one I want. Thanks in advance
Don't think that saving the file to document directory will generate you new name.
When I save something to documents I generate unique file name.
What I do is I pass to web server some generic name like "Upload.png".
Here is some pseudo code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
...
NSData *data = UIImagePNGRepresentation(choosenImage);
NSString *name = #"Uploaded.png"
// Upload the nsdata to server
[[PHPServer instance] uploadProfileImage:data named:name];
}

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.

Resources