存储对保存在相机胶卷中的图像的引用

人气:872 发布:2022-10-16 标签: iphone save photo uiimagepickercontroller

问题描述

我一直在开发一个简单的应用程序,它允许用户拍摄照片并将其存储在相机胶卷中以供以后使用.为此,我使用了 UIImagePickerController 和 UIImageWriteToSavedPhotosAlbum 方法,该方法运行良好.

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?

如果有一种简单的方法可以做到这一点,那就太好了,因为有一个视频,您只需存储特定视频的 NSUrl,然后调用 MPMoviePlayerController 为您做所有事情.

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.

任何帮助将不胜感激!

推荐答案

原来做这个其实没那么难.在 UIImagePickerDelegate 方法中:

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

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

NSDictionary信息"实际上包含存储在相机胶卷上的图像或电影的 url.您只需要检查:

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

这会将电影保存到相机胶卷,并记录网址.为照片做类似,只需将kUTTypeMovie"替换为kUTTypeImage",然后替换

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.

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

如果您需要将 UIImage 不存储在相机胶卷上,stackoverflow 上有一篇很棒的帖子 使用 NSCoding 保存 UIImage,您可以使用它来将图像存储在您的应用中.希望对某人有所帮助!

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!

584