iPhone - UIImagePickerController -> save the image to app folder
来源:互联网 发布:js将数组转换成json 编辑:程序博客网 时间:2024/06/07 01:48
http://stackoverflow.com/questions/4957972/iphone-uiimagepickercontroller-save-the-image-to-app-folder
4down votefavorite
Hello!
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.
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.
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.
3 Answers
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];
}
}
That's all
Depending on the file format you want to save, you can use
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
or [UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];
.
This is the code to save the UIImage into the document directory. You can use this code in didFinishPickingImage delegate method:
// Create paths to output images
NSString *pngPath = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
EDIT
You can also use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
to find the path to your application document directory, instead of NSHomeDierctory.
- iPhone - UIImagePickerController -> save the image to app folder
- iPhone开发之-save image to adressbook
- Save Image to UserDefaults
- You don’t have permission to save the file “project.xcworkspace” in the folder
- ios save image to album
- UIImagePickerController Save to Disk then Load to UIImageView
- How to resize a *.jpg image and save the result to a file
- WJ的Direct3D简明教程1:Save Screen To the Image
- SSIS: Adding Files To The Miscellaneous Folder
- The scripts to find Folder details
- matlab cannot save changes to the path
- IDl save the command line to file
- image to DataBase and show the image
- Failed to save the updated manifest to the file
- Failed to save the updated manifest to the file
- Failed to save the updated manifest to the file
- Failed to save the updated manifest to the file
- Migrating Folder Property sample app to Vault 2012
- 战胜Facebook的方法
- 笔记:MySQL Spatial Extensions
- UIImagePickerController Save to Disk then Load to UIImageView
- PARTITION RANGE SUBQUERY子查询消除
- Java学习从入门到精通[原创]自我感觉特别好
- iPhone - UIImagePickerController -> save the image to app folder
- IE8不能运行JavaScript
- Get iPhone Status Bar Height
- Excel表数据导入Sql Server数据库中
- 恢复或者隐藏IE地址栏
- [Android]调用字符串资源的方法
- 深入理解iPhone委托模式兼谈iPhone生命周期
- python学习--正则表达式(一)
- 今天对乔帮主的期待越高 明天对苹果的失望就越多