Storing a reference to an image saved in the Camer

2020-03-01 00:57发布

问题:

I've been working on a simple application which allows the user to take a photo and store it in the camera roll for later use. To do this, I am using a UIImagePickerController, and the method UIImageWriteToSavedPhotosAlbum, which is working perfectly.

My question is: Is there any way to store the path to the image in the Camera Roll so that I can use that to call the image again later? Or, do I have to save the image in the app in order to use it again later?

It would be great if there was a simple way to do this, as there is with a video where you just store the NSUrl for the particular video, and then call MPMoviePlayerController to do everything for you.

Any help would be much appreciated!

回答1:

It turns out that it is actually not that hard to do this. In the UIImagePickerDelegate method:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

the NSDictionary "info" actually contains the url to the image or the movie which is stored on the Camera Roll. You need only check:

if([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]){ // If the user took a video,
  movieURL = [[info valueForKey:UIImagePickerControllerMediaURL] retain];// Get the URL of where the movie is stored.
  UISaveVideoAtPathToSavedPhotosAlbum([movieURL path], nil, nil, nil);   // Save the movie to the photo album.
}

and this will save the movie to the Camera Roll, as well as record the url. Doing it for a photo is analogous, just replace the "kUTTypeMovie" with "kUTTypeImage", and replace

movieURL = [[info valueForKey:UIImagePickerControllerMediaURL] retain];// Get the URL of where the movie is stored.
UISaveVideoAtPathToSavedPhotosAlbum([movieURL path], nil, nil, nil);   // Save the movie to the photo album.

with

UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];    
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

If you need to store the UIImage not on the Camera Roll, there is a great post on stackoverflow Saving UIImage with NSCoding that you can use to store the image in your app. Hope that helps someone!