【IOS开发】UIImage 和 NSString的保存

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

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


UIImagePickerController中的常用属性:

@property(nonatomic) BOOL allowsEditing

  是否允许编辑选中的图片


@property(nonatomic) UIImagePickerControllerSourceType sourceType

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

enum {
   UIImagePickerControllerSourceTypePhotoLibrary ,
   UIImagePickerControllerSourceTypeCamera ,
   UIImagePickerControllerSourceTypeSavedPhotosAlbum 
};
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


#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) UIImageView *imageView;

@end

@implementation ViewController
/*
 需求:
 
 从照片库读取一张照片,并且设置界面上的UIImageView
 再次进入应用时,直接显示上次选择的图像
 
 思路:
 1. 界面上需要一个按钮和一个UIImageView
 2. 使用UIImagePickerController选择照片并且设置UIImageView的显示
 3. 保存用户选择的UIImage
 */

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置界面
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 300)];
    [imageView setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:imageView];
    _imageView = imageView;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(110, 320, 100, 40)];
    [button addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    // 1. 判断文件是否存在
    NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [documents[0] stringByAppendingPathComponent:@"image.png"];
    // 注意:此处不要使用imageNamed方法,因为imageNamed方法是从bundle中加载图像的
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    
    // 2. 如果存在加载图像并且显示在UIImageView中
    if (image != nil) {
        [imageView setImage:image];
    }
    
    // 从磁盘加载文本文件到字符串
    NSString *strPath = [documents[0] stringByAppendingPathComponent:@"123.txt"];
    // 关于Error参数:
    // 1. 看到autoreleasing描述符,需要实例化一个指针,并且传入指针的地址
    NSError *error = nil;
    
    NSString *string = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"string %@", string);
    
//    // 3. 写入NSString的演示
//    NSString *string = @"爱老虎油!!!";
//    // 除非特殊原因,在iOS开发中字符串的编码格式,统一使用UTF8编码
//    [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

#pragma mark UIImagePicker代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"%@", info);
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    [_imageView setImage:image];
    
    // 保存图像时,需要使用NSData进行中转,NSData中间可以存储任意格式的二进制数据
    // 1. 将UIImage转换成NSData
    NSData *imageData = UIImagePNGRepresentation(image);
    // 2. 建立保存文件的路径
    NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [documents[0] stringByAppendingPathComponent:@"image.png"];
    // 3. 将NSData写入文件
    [imageData writeToFile:path atomically:YES];
    
    // 4. 将NSString写入文件
    NSString *string = @"老虎爱油!!!";
    NSString *strPath = [documents[0] stringByAppendingPathComponent:@"123.txt"];
    [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    // 5. 将info写入Documents保存
    NSString *infoPath = [documents[0] stringByAppendingPathComponent:@"info.plist"];
    [info writeToFile:infoPath atomically:YES];
    NSLog(@"info写入成功吗?");
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 按钮监听方法
- (void)selectPhoto
{
    NSLog(@"touch me baby");
    // 1. 实例化照片选择器
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    // 2. 设置属性
    [imagePicker setAllowsEditing:YES];
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [imagePicker setDelegate:self];
    
    [self presentViewController:imagePicker animated:YES completion:nil];
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值