我应该在哪里存储在iPhone上选择的图像?

时间:2009-03-18 14:41:10

标签: iphone objective-c cocoa-touch

我想拍摄照片或从用户现有照片中选择现有照片。做这个的最好方式是什么?

具体而言,我担心应该在哪里存储图像。存储位置应该是应用程序的私有位置。每次打开应用程序时,我都需要能够将图像重用为背景。谢谢!

2 个答案:

答案 0 :(得分:3)

您应该使用UIImagePickerController从库或相机中检索图像。您可以将图片保留在App的Documents文件夹中。此文件夹是您的应用专用的,可以写入。

答案 1 :(得分:3)

您可以像这样获取文档文件夹的路径

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *docDirectory = [sysPaths objectAtIndex:0];

并在那里保存文件。

NSString *filePath = [NSString stringWithFormat:@"%@whatever.jpg",docDirectory];
NSData *toSave = UIImageJPEGRepresentation(image,1.0); //image is a UIImage
[toSave writeToFile:filePath atomically:YES];