iPhone - UIImagePickerController - >将图像保存到app文件夹 [英] iPhone - UIImagePickerController -> save the image to app folder
问题描述
我有一个使用 UIImagePickerController
的iPhone应用程序。如 sourceType
我有
I have an iPhone application using a UIImagePickerController
. As sourceType
I have
- UIImagePickerControllerSourceTypePhotoLibrary
- UIImagePickerControllerSourceTypeCamera
- UIImagePickerControllerSourceTypeSavedPhotosAlbum
因此用户可以制作照片或从照片中选择一张照片来自照片或相机照片的库。
so the user can make a photo or select one from the photo library from the photos or camera photos.
图像将显示在UIImageView中。如果用户关闭应用程序,则应保存图像。因此,对于文本字段,我使用 NSUserDefaults
。我知道,使用NSData将图像保存在NSUSerDefaults中并不是一个好方法,所以我想将图像保存/复制到由我的应用程序控制的文件夹,类似于NSUserDefaults。
The image will be displayed in a UIImageView. The image should be saved if the user closes the app. So for text fields, I use NSUserDefaults
. I know, it is not a good way to save the image inside the NSUSerDefaults with NSData, so I want to save / copy the image to a folder which is controlled by my application similar to the NSUserDefaults.
我该怎么做?我想保存它,然后我可以将文件的路径写入我的 NSUserDefaults
并在启动应用程序时读取它。
How can I do this? I want to save it, and then I can write the path to the file into my NSUserDefaults
and read it on startup of the application.
提前谢谢你&最好的问候。
Thank you in advance & Best Regards.
推荐答案
您可以在 UIImagePickerControllerDelegate
委托中使用以下代码实现
You can use the following code in UIImagePickerControllerDelegate
delegate implementation
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
}
这都是
这篇关于iPhone - UIImagePickerController - >将图像保存到app文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!