In the UIImagePickerController, is it possible to get the NSURL value for UIImagePickerControllerMediaURL?
When the user selects an image from the picker controller, I'm calling the delegate:
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
in the dictionar开发者_JAVA百科y info, the value for key UIImagePickerControllerMediaURL
is NULL
. Am I missing something here?
I know this question is horribly old, but I did manage to wander across it from a recent google search, so thought I'd throw in what I think is actually the answer to your question which the other answers seem to miss.
You're getting NULL for UIImagePickerControllerMediaURL as it is only returned for the Movie (kUTTypeMovie) media type in a picker. If you're presenting a default picker without specifying additional media types after it's creation, then the docs seem to imply that you won't get it:
"UIImagePickerControllerMediaURL - Specifies the filesystem URL for the movie."
And I can confirm that it isn't returned at least in the quick test that I did in iOS 6 for images (not Movies).
To access the image returned, you actually have direct access to a UIImage object under the keys of UIImagePickerControllerOriginalImage (if editing is not allowed, the default setting) or UIImagePickerControllerEditedImage if editing is allowed in the picker. The original image is always available, even if editing was done in the picker. You can treat these UIImage's just like you would any other UIImage in your program.
You can get an NSURL path to the object, but it isn't quite what you would expect. And I can't seem to access the data in the file either.
- (void)imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// dismiss the image picker modal
[[imagepicker parentViewController] dismissModalViewControllerAnimated:YES];
// grab the image url
NSURL *imageURL = [info valueForKey: UIImagePickerControllerReferenceURL];
// get the data from the url
NSData *data = [NSData dataWithContentsOfURL:imageURL];
// data is null at this point
}
The NSURL looks something like the following for a jpeg file.
assets-library://asset/asset.JPG?id=1000000246&ext=JPG
Yes the Meta data is not populated in the callback (some were also expecting the geotag / position of the place where the picture was taken , but this is not the case). The fact that you don't get the NSURL sounds logical from Apple perspective : they will not give you the path to the real file (like file:// ....) because this is something that is considered as part of the implementation, not part of the public API : from the client perspective it looks like if apple doesn't want you to known where the picture is store physically on the File system.
Running iOS 3.2.1 on an iPad, it seems that my app is able to get the file URL via UIImagePickerControllerMediaURL
for image files, even though the documentation seems to say that it only works for movie files.
The following link helped me. display image from URL retrieved from ALAsset in iPhone
The UIImagePickerControllerReferenceURL
returns an asset-library url. You need to use an ALAssetLibrary object to access the image. The link describes how to do this.
精彩评论