如何在iPhone相机胶卷上取回已保存的图片? [英] How do I get back a saved picture on iPhone camera roll?
问题描述
我是iOS开发的新手,我正在尝试创建一个允许用户使用相机拍照的视图。
I am new to iOS development and am trying to create a view that will allow the user to take a picture using their camera.
我想要发生的是当用户拍摄照片时,它会将其保存到相机胶卷中。我相信可以通过以下声明简单地完成:
What I want to happen is when the user has taken the picture, it saves it to their camera roll. I believe that can be simply accomplished by the following statement:
//Let's say the image you want to save is in a UIImage called "imageToBeSaved"
UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);
但是,我的问题是,如果用户离开视图然后返回到它,我仍然希望图片在那里。那么我如何取回之前拍摄/保存的相同图片,以便在重新打开视图时重新加载?
However, my question is, if the user leaves the view and then returns to it I still want that picture to be there. So how do I get back the same picture I have just taken/saved previously, so I can reload it when they reopen the view?
任何帮助都会很棒!谢谢。
Any help would be great! Thank you.
推荐答案
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
void UIImageReadFromFile(UIImage **image, NSString *fileName)
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
image = [UIImage imageWithContentsOfFile:filePath];
}
图像将保存到应用程序包的Documents目录中并从中读取指定名称。
Image will be saved to and read from the Documents directory of your app's bundle with specified name.
用法示例:
UIImageWriteToFile(image, @"somephoto.png");
UIImage *fetchedImage;
UIImageReadFromFile(&fetchedImage, @"somephoto.png");
这篇关于如何在iPhone相机胶卷上取回已保存的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!