ios选择相册图片并保存

.h中: 
Obj-c代码  收藏代码
  1. <UINavigationControllerDelegate, UIImagePickerControllerDelegate>  


.m中 
Obj-c代码   收藏代码
  1. - (IBAction)selectPhoto {  
  2.     NSLog(@"%@",NSStringFromSelector(_cmd));  
  3.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {  
  4.         UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
  5.         picker.delegate = self;  
  6.         picker.allowsEditing = YES;  
  7.         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  8.         [self presentModalViewController:picker animated:YES];  
  9.         [picker release];  
  10.     }  
  11.     else {  
  12.         UIAlertView *alert = [[UIAlertView alloc]   
  13.                               initWithTitle:@"连接到图片库错误"   
  14.                               message:@""   
  15.                               delegate:nil   
  16.                               cancelButtonTitle:@"确定"   
  17.                               otherButtonTitles:nil];  
  18.         [alert show];  
  19.         [alert release];  
  20.     }  
  21.       
  22. }  
  23.   
  24. #pragma mark UIImagePickerControllerDelegate  
  25. - (void)imagePickerController:(UIImagePickerController *)picker   
  26.         didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo   
  27. {  
  28.     [self.headImageView setImage:image];  
  29.     if ([self writeImage:image toFileAtPath:[self imageFilePath]]) {  
  30.         NSLog(@"Save Image Success!!!");  
  31.     }  
  32.       
  33.     [picker dismissModalViewControllerAnimated:YES];  
  34.       
  35. }  
  36.   
  37. - (NSString *)imageFilePath  
  38. {  
  39.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  40.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  41.     return [documentsDirectory stringByAppendingPathComponent:@"image.png"];  
  42. }  
  43.   
  44. - (BOOL)writeImage:(UIImage*)image toFileAtPath:(NSString*)aPath  
  45. {  
  46.     if ((image == nil) || (aPath == nil) || ([aPath isEqualToString:@""]))  
  47.         return NO;  
  48.     @try  
  49.     {  
  50.           
  51.         NSData *imageData = nil;  
  52.         NSString *ext = [aPath pathExtension];  
  53.         if ([ext isEqualToString:@"png"])  
  54.         {  
  55.             imageData = UIImagePNGRepresentation(image);   
  56.         }  
  57.         else   
  58.         {  
  59.             // the rest, we write to jpeg  
  60.               
  61.             // 0. best, 1. lost. about compress.  
  62.               
  63.             imageData = UIImageJPEGRepresentation(image, 0);       
  64.               
  65.         }  
  66.        
  67.         if ((imageData == nil) || ([imageData length] <= 0))  
  68.             return NO;  
  69.           
  70.         [imageData writeToFile:aPath atomically:YES];         
  71.         return YES;  
  72.     }  
  73.     @catch (NSException *e)   
  74.     {  
  75.         NSLog(@"create thumbnail exception.");  
  76.     }  
  77.     return NO;  
  78. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值