I have two view controllers, in one I pick the image from the photo library and assign it to an image property. With this picker:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
But instead I would like to just get the address/data of the image from the picker in the first controller, push the next view controller and then open the image with that address there. So then I do the following:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
When I print info of the photo it looks something like this:
assets-library://asset/asset.JPG?id=52219875-C221-4F60-B9D4-984AAADB6E63&ext=JPG
The problem is that UIImage has methods initWithData or imageWithData but these accept NSData and not NSDictionary.
How can I open an image from a photo library using its data?
This method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)img
editingInfo:(NSDictionary *)editInfo
shouldn't be used, it's been deprecated since ios3
This method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
returns a dictionary, as you report.
The image pointer is one of the objects in this dictionary. You obtain it as such:
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
Here's Apple's documentation for that method.
Basically there are a few keys in that dictionary, listen here:
NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;
You probably want UIImagePickerControllerEditedImage. So this is what you're looking for:
UIImage *theImage = [info valueForKey:UIImagePickerControllerEditedImage];
Of course you can get other info from the image with the other keys in the dictionary too.
The dictionary provides the URL for the image.
So load the data by
NSData *imageData = [NSData dataWithContentsOfURL:[info valueForKey: UIImagePickerControllerReferenceURL]];
and use this data for the NSImage initWithData method.
Related
When I click an image in my camera application it will give me an image path in console.
That image path will be save in my sqlite database and that image will also be saved in a separate folder in my phone gallery.
using picker delegate. you can store the path in your db as well as write image to your folder from the path.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL* imageUrl = [info valueForKey:UIImagePickerControllerMediaURL];
selectedImage = [imageUrl path];
NSLog(#"selectedImage: %#", selectedImage);
}
All
I have a requirement to Upload a UIImage taken from Photo Gallery or Photo taken from Camera,
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:chosenImage,#"chosenImage", nil];
UIImage* selectedImage = [userInfo objectForKey:#"chosenImage"]
NSData *imageData = UIImagePNGRepresentation(selectedImage);
}
I am using imageData to upload it on server, I want to know is there any way to reduce the memory size of the UIImage without changing the quality. Please let me know.
I have code working that lets user select an existing image and have it displaying nicely in UIView on screen.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
NSLog(#"image is%#",chosenImage);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
How do I get the name of the file to persist it for future reference? chosenImage just shows you a bunch of metadata about the file.
Thank you.
As you see below, you can't get the name of the selected image from UIImagePickerControllerDelegate using the currently existing API.
Here are UIImagePickerControllerDelegate's available Editing Information Keys:
NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;
However, you may want to make use of AssetsLibrary.framework to obtain the name of an image managed by Photos application:
#import AssetsLibrary;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:info[UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *fileAsset) {
NSLog(#"%#", [[fileAsset defaultRepresentation] filename]);
} failureBlock:nil];
}
try NSLog(#"%#",info) at the beging of that method and see if the name is in the dictionary.
In my application, i need to select mp4 video file from iPhone Gallery and upload it on server.Is it possible to check whether the selected file is .mp4 or .mov?
I got the file extension from the below snippet
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSString * videoURL=info[UIImagePickerControllerReferenceURL];
NSString *lastPath = [videoURL lastPathComponent];
NSString *fileExtension = [lastPath pathExtension];
NSLog(#"File extension %#",fileExtension);
}
}
No we cant read it there by selecting.once you selected it, that file will came in to your app there you decode to NSdata and encode it back.in between this process there is a chance to find out.try this method, Hope this will work.
I'm now struggling with UIImagePicker.
I've read the document but it seems not to mention how should I save a newly token photo, I mean save it into my own documents in my app.
In fact, there is a "photo" attribute in one of my entity in core data,
so, the "photo" should just be a NSString to save the real photo's file path, right?
But how to save that photo and get its correct file path?
I just know little about NSFilePath or something like that, hope someone to teach me a little bit.
Thanks a lot!
You may know how to present the image picker:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[viewController presentModalViewController:picker animated:YES];
With the above the viewController implements the UIImagePickerControllerDelegate and make sure you implement this function:
- (void)imagePickerController:(UIImagePickerController *)returnedPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
and
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)returnedPicker
The first method is called when user has picked a photo. In this method you get the image using:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
To write this to a file you can make it a JPEG or a PNG. If a JPEG you may do this:
[UIImageJPEGRepresentation(image, 1.0) writeToFile:filename atomically:YES];
You just need to set the filename.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
NSString *filename = [docsDir stringByAppendingPathComponent:#"myFilename.jpg"];
To get the image back into a UIImage you do this:
NSData *data = [NSData dataWithContentsOfFile:filename;
if(data != nil)
UIImage *image = [UIImage imageWithData:data];