iPhone - UIImagePickerController -> save the image to app folder
2011-06-06 22:29
615 查看
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 sourceTypeI 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 NSUserDefaultsand read it on startup of the application. Thank you in advance & Best Regards. iphone objective-c uiimage uiimagepickercontroller
| |||
3 Answers
activeoldestvotesup vote3down voteaccepted | You can use the following code in UIImagePickerControllerDelegatedelegate 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
| |||
up vote1down vote | 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];.
| ||
up vote0down vote | 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.
|
相关文章推荐
- Attempt to present <UIImagePickerController: 0x1848de00> on <UINavigationController: 0x17e6ed50> wh
- Warning: Attempt to present <UIImagePickerController: 0x292b400> on xxx which is already presenti
- ios错误<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set
- Assigning to ‘id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>’ from incompatible
- Send an image from the iPhone using ASIHTTP and UIImagePicker
- VS2008 1>mt.exe : general error c101008a: Failed to save the updated manifest to the file
- How to draw a shape on top of a UIImage while respecting the image's alpha mask
- CentOS学习笔记1:< yum提示another app is currently holding the yum lock;waiting for it to exit >
- VS2008 1>mt.exe : general error c101008a: Failed to save the updated manifest to the file
- UIImagePickerController拍照后黑屏,报错<Error>: CGAffineTransformInvert: singular matrix.
- <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_C
- iOS - 解决Warning: Attempt to present <UIImagePickerController: 0x7f9ba106a000>which is already presen
- UIImagePickerController Save to Disk then Load to UIImageView
- <the hacker guide to python>读书笔记
- iOS 最新提示Unable to add App ID because the '10' App ID limit in '7' days has been exceeded.
- iphone 怎么使用图片选取器 UIImagePickerController
- UIImagePickerController 图像选取器--在iPhone中调用照相机、照片库
- iphone UIImagePickerController组件图片旋转问题
- How to make two <div>…</div> in the same row?
- iphone navigationController : wait for uialertview response before to quit the current view