保存到相册以及写入document目录以及读取示例

  1. @interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate> 
  2.  
  3. @property (retain, nonatomic) IBOutlet UIImageView *imageView; 
  4. @property (retain, nonatomic) UIButton *saveToFileButton; 
  5.  
  6. //打开相册 
  7. - (IBAction)openAlbum:(id)sender; 
  8.  
  9. //从文件夹读取图片 
  10. - (IBAction)readImage:(id)sender; 
  11.  
  12. @end 
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (retain, nonatomic) UIButton *saveToFileButton;

//打开相册
- (IBAction)openAlbum:(id)sender;

//从文件夹读取图片
- (IBAction)readImage:(id)sender;

@end


下面是ViewController.m文件

  1. #import "ViewController.h" 
  2. //保存到文件夹按钮的标签,选取图片前,这个按钮是隐藏的 
  3. #define SAVE_BUTTON_TAG 101 
  4.  
  5. @interface ViewController () 
  6.  
  7. @end 
  8.  
  9. @implementation ViewController 
  10. @synthesize imageView = _imageView; 
  11. @synthesize saveToFileButton = _saveToFileButton; 
  12.  
  13. - (void)viewDidLoad 
  14.     [super viewDidLoad]; 
  15.     //根据设置的tag获取按钮控件 
  16.     self.saveToFileButton = (UIButton *)[self.view viewWithTag:SAVE_BUTTON_TAG]; 
  17.     //添加对象事件 
  18.     [self.saveToFileButton addTarget:self action:@selector(saveToFileBtnTapped:) forControlEvents:UIControlEventTouchUpInside]; 
  19.     //设置为不可见 
  20.     self.saveToFileButton.hidden = YES; 
  21.  
  22. - (void)viewDidUnload 
  23.     [self setImageView:nil]; 
  24.     [self setSaveToFileButton:nil]; 
  25.     [super viewDidUnload]; 
  26.     // Release any retained subviews of the main view. 
  27.  
  28. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
  29.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
  30.  
  31. - (void)dealloc { 
  32.     [self.imageView release]; 
  33.     [self.saveToFileButton release]; 
  34.     [super dealloc]; 
  35.  
  36. - (IBAction)openAlbum:(id)sender { 
  37.     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil]; 
  38.     [actionSheet showInView:self.view]; 
  39.     [actionSheet release]; 
  40.  
  41. //从文件夹读取图片 
  42. - (IBAction)readImage:(id)sender { 
  43.     NSString *imagePath = [self imageSavedPath:@"image.png"]; 
  44.     NSFileManager *fileManager = [NSFileManager defaultManager]; 
  45.     //判断文件是否存在 
  46.     if (![fileManager fileExistsAtPath:imagePath]) { 
  47.         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Note" message:@"文件不存在" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
  48.         [alertView show]; 
  49.         [alertView release]; 
  50.     }else
  51.         //从指定目录读取图片 
  52.         UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; 
  53.         self.imageView.image = image; 
  54.     } 
  55.  
  56. //保存到文件按钮事件 
  57. - (void)saveToFileBtnTapped:(id)sender { 
  58.     NSString *imagePath = [self imageSavedPath:@"image.png"]; 
  59.     BOOL isSaveSuccess = [self saveToDocument:self.imageView.image withFilePath:imagePath]; 
  60.      
  61.     if (isSaveSuccess) { 
  62.         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作结果" message:@"保存图片成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
  63.         [alertView show]; 
  64.         [alertView release]; 
  65.     }else
  66.         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作结果" message:@"保存图片失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
  67.         [alertView show]; 
  68.         [alertView release]; 
  69.     } 
  70.  
  71. //将选取的图片保存到目录文件夹下 
  72. -(BOOL)saveToDocument:(UIImage *) image withFilePath:(NSString *) filePath 
  73.     if ((image == nil) || (filePath == nil) || [filePath isEqualToString:@""]) { 
  74.         return NO; 
  75.     } 
  76.      
  77.     @try
  78.         NSData *imageData = nil; 
  79.         //获取文件扩展名 
  80.         NSString *extention = [filePath pathExtension]; 
  81.         if ([extention isEqualToString:@"png"]) { 
  82.             //返回PNG格式的图片数据 
  83.             imageData = UIImagePNGRepresentation(image); 
  84.         }else
  85.             //返回JPG格式的图片数据,第二个参数为压缩质量:0:best 1:lost 
  86.             imageData = UIImageJPEGRepresentation(image, 0); 
  87.         } 
  88.         if (imageData == nil || [imageData length] <= 0) { 
  89.             return NO; 
  90.         } 
  91.         //将图片写入指定路径 
  92.         [imageData writeToFile:filePath atomically:YES]; 
  93.         return  YES; 
  94.     } 
  95.     @catch (NSException *exception) { 
  96.         NSLog(@"保存图片失败"); 
  97.     } 
  98.      
  99.     return NO; 
  100.      
  101.  
  102. //根据图片名将图片保存到ImageFile文件夹中 
  103. -(NSString *)imageSavedPath:(NSString *) imageName 
  104.     //获取Documents文件夹目录 
  105.     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  106.     NSString *documentPath = [path objectAtIndex:0]; 
  107.     //获取文件管理器 
  108.     NSFileManager *fileManager = [NSFileManager defaultManager]; 
  109.     //指定新建文件夹路径 
  110.     NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"]; 
  111.     //创建ImageFile文件夹 
  112.     [fileManager createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil]; 
  113.     //返回保存图片的路径(图片保存在ImageFile文件夹下) 
  114.     NSString * imagePath = [imageDocPath stringByAppendingPathComponent:imageName]; 
  115.     return imagePath; 
  116.  
  117. #pragma Delegate method UIImagePickerControllerDelegate 
  118. //图像选取器的委托方法,选完图片后回调该方法 
  119. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
  120.     if (image != nil) { 
  121.         //选定照片后在界面显示照片并把保存按钮设为可见 
  122.         self.imageView.image = image; 
  123.         self.saveToFileButton.hidden = NO; 
  124.     } 
  125.     //关闭图像选择器 
  126.     [self dismissModalViewControllerAnimated:YES]; 
  127.  
  128. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
  129.     //获取图片选取器 
  130.     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
  131.     //指定代理 
  132.     imagePicker.delegate = self; 
  133.     //打开图片后允许编辑 
  134.     imagePicker.allowsEditing = YES; 
  135.      
  136.     //判断图片源的类型 
  137.     if (buttonIndex == 0) { 
  138.         if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
  139.             //相机 
  140.             imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
  141.         } 
  142.     }else if (buttonIndex == 1) { 
  143.         if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ 
  144.             //图片库 
  145.             imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
  146.         } 
  147. //        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { 
  148. //            //相册 
  149. //            imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
  150. //        } 
  151.     }else if (buttonIndex == [actionSheet cancelButtonIndex]) { 
  152.         return
  153.     } 
  154.      
  155.     //打开图片选择模态视图 
  156.     [self presentModalViewController:imagePicker animated:YES]; 
  157.     [imagePicker release]; 
  158.  
  159.  
  160. @end 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值