图片序列化和反序列化成图片文件(代码)
生活随笔
收集整理的這篇文章主要介紹了
图片序列化和反序列化成图片文件(代码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//
// 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
?
轉載于:https://www.cnblogs.com/sivek/p/6550579.html
總結
以上是生活随笔為你收集整理的图片序列化和反序列化成图片文件(代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决webview调用 goBack()
- 下一篇: 语法之知识点的改进