图片序列化和反序列化成图片文件(代码)

//
//  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

 

posted @ 2017-03-14 20:48  Sivek_lin  阅读(1835)  评论(0编辑  收藏  举报