【IOS开发】UIImage 和 NSString的保存

主要用的函数和方法上一篇中都有介绍,这里就不在重复了。如果有需要可以自行阅读前文:

这里主要讲解如何使用UIImagePickerController选择照片,显示在UIImageView中并且保存到沙盒中,当下一次在打开应用的时候,可以直接提取。


UIImagePickerController中的常用属性:

@property(nonatomic) BOOL allowsEditing

  是否允许编辑选中的图片


@property(nonatomic) UIImagePickerControllerSourceType sourceType

  设置照片选择器的展示样式

[objc]  view plain copy
  1. enum {  
  2.    UIImagePickerControllerSourceTypePhotoLibrary ,  
  3.    UIImagePickerControllerSourceTypeCamera ,  
  4.    UIImagePickerControllerSourceTypeSavedPhotosAlbum   
  5. };  
  6. typedef NSUInteger  UIImagePickerControllerSourceType;  


- ( void ) presentViewController: ( UIViewController * ) viewControllerToPresent
       animated: ( BOOL ) flag
       completion: ( void (^)(void) ) completion
 展现相关的ViewController。

- (void)dismissViewControllerAnimated:(BOOL)flag
                           completion:(void (^)(void))completion

 关闭相关的ViewController。

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info


[objc]  view plain copy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @property (weak, nonatomicUIImageView *imageView;  
  6.   
  7. @end  
  8.   
  9. @implementation ViewController  
  10. /* 
  11.  需求: 
  12.   
  13.  从照片库读取一张照片,并且设置界面上的UIImageView 
  14.  再次进入应用时,直接显示上次选择的图像 
  15.   
  16.  思路: 
  17.  1. 界面上需要一个按钮和一个UIImageView 
  18.  2. 使用UIImagePickerController选择照片并且设置UIImageView的显示 
  19.  3. 保存用户选择的UIImage 
  20.  */  
  21.   
  22. - (void)viewDidLoad  
  23. {  
  24.     [super viewDidLoad];  
  25.       
  26.     // 设置界面  
  27.     UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(1010300300)];  
  28.     [imageView setBackgroundColor:[UIColor lightGrayColor]];  
  29.     [self.view addSubview:imageView];  
  30.     _imageView = imageView;  
  31.       
  32.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  33.     [button setFrame:CGRectMake(11032010040)];  
  34.     [button addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];  
  35.       
  36.     [self.view addSubview:button];  
  37.       
  38.     // 1. 判断文件是否存在  
  39.     NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  40.     NSString *path = [documents[0] stringByAppendingPathComponent:@"image.png"];  
  41.     // 注意:此处不要使用imageNamed方法,因为imageNamed方法是从bundle中加载图像的  
  42.     UIImage *image = [UIImage imageWithContentsOfFile:path];  
  43.       
  44.     // 2. 如果存在加载图像并且显示在UIImageView中  
  45.     if (image != nil) {  
  46.         [imageView setImage:image];  
  47.     }  
  48.       
  49.     // 从磁盘加载文本文件到字符串  
  50.     NSString *strPath = [documents[0] stringByAppendingPathComponent:@"123.txt"];  
  51.     // 关于Error参数:  
  52.     // 1. 看到autoreleasing描述符,需要实例化一个指针,并且传入指针的地址  
  53.     NSError *error = nil;  
  54.       
  55.     NSString *string = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:&error];  
  56.     NSLog(@"string %@", string);  
  57.       
  58. //    // 3. 写入NSString的演示  
  59. //    NSString *string = @"爱老虎油!!!";  
  60. //    // 除非特殊原因,在iOS开发中字符串的编码格式,统一使用UTF8编码  
  61. //    [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];  
  62. }  
  63.   
  64. #pragma mark UIImagePicker代理方法  
  65. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  66. {  
  67.     NSLog(@"%@", info);  
  68.     UIImage *image = info[@"UIImagePickerControllerEditedImage"];  
  69.     [_imageView setImage:image];  
  70.       
  71.     // 保存图像时,需要使用NSData进行中转,NSData中间可以存储任意格式的二进制数据  
  72.     // 1. 将UIImage转换成NSData  
  73.     NSData *imageData = UIImagePNGRepresentation(image);  
  74.     // 2. 建立保存文件的路径  
  75.     NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  76.     NSString *path = [documents[0] stringByAppendingPathComponent:@"image.png"];  
  77.     // 3. 将NSData写入文件  
  78.     [imageData writeToFile:path atomically:YES];  
  79.       
  80.     // 4. 将NSString写入文件  
  81.     NSString *string = @"老虎爱油!!!";  
  82.     NSString *strPath = [documents[0] stringByAppendingPathComponent:@"123.txt"];  
  83.     [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];  
  84.       
  85.     // 5. 将info写入Documents保存  
  86.     NSString *infoPath = [documents[0] stringByAppendingPathComponent:@"info.plist"];  
  87.     [info writeToFile:infoPath atomically:YES];  
  88.     NSLog(@"info写入成功吗?");  
  89.       
  90.     [picker dismissViewControllerAnimated:YES completion:nil];  
  91. }  
  92.   
  93. #pragma mark 按钮监听方法  
  94. - (void)selectPhoto  
  95. {  
  96.     NSLog(@"touch me baby");  
  97.     // 1. 实例化照片选择器  
  98.     UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];  
  99.     // 2. 设置属性  
  100.     [imagePicker setAllowsEditing:YES];  
  101.     [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];  
  102.     [imagePicker setDelegate:self];  
  103.       
  104.     [self presentViewController:imagePicker animated:YES completion:nil];  
  105. }  
  106.   
  107. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值