// // ViewController.m // 图片序列化和反序列化 // // Created by Qlinchao on 17/3/14. // Copyright © 2017年 QLC. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *path = [dirArray firstObject]; path = [path stringByAppendingPathComponent:@"imageTest.jpg"]; UIImage *image = [UIImage imageNamed:@"login_register_background.png"]; NSData *imageData = UIImageJPEGRepresentation(image, 1.0); BOOL isWrite = [imageData writeToFile:path atomically:YES]; if (isWrite) { NSLog(@"写入成功"); } else { NSLog(@"写入失败"); } } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self readFile]; } -(void)readFile{ NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *path = [dirArray firstObject]; path = [path stringByAppendingPathComponent:@"imageTest.jpg"]; NSLog(@"%@",path); if([[NSFileManager defaultManager] fileExistsAtPath:path]){ NSData *picData = [NSData dataWithContentsOfFile:path]; self.imageView.image = [UIImage imageWithData:picData]; } } @end