#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark ---图片--------
//根据ImageNamed获取图片(会在缓存中存储一份,下次再获取同名图片的话直接从缓存中取出) 优点:快 只有第一次时慢 缺点:会浪费内存,如果只用一次的话这块内存就会浪费掉了
//UIImage *image = [UIImage imageNamed:@"1"];
//根据ContentOfFile获取图片:每一次都会从路径中读取,不会占用内存.如果该图片只使用一次的话 推荐使用ContentOfFile
//UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:@"1"];
//123.png
//123@2X.png:两倍分辨率 5,5s,6,6s
//123@3X.png:三倍分辨率 6plus ,6s plus
//第一步:创建一个图片对象
UIImage *image = [UIImage imageNamed:@"1"];
#pragma mark ---将UIImage类型对象转化成NSData类型---
//第一个参数:表示转哪个UIImage类型的对象
//第二个参数:压缩系数 越小压缩的越厉害
//第二步:将图片对象装换成NSData对象
NSData *data = UIImageJPEGRepresentation(image, 1);
//第三步:确定要存储文件夹的路径
NSString *documentPathStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
//第四步:确定要存储的文件
NSString *dataPath = [documentPathStr stringByAppendingPathComponent:@"data.png"];
//第五步:写入
[data writeToFile:dataPath atomically:YES];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
#pragma mark ---将类型NSData对象转化成UIImage类型---
NSData *newData = [NSData dataWithContentsOfFile:dataPath];
UIImage *newImage = [[UIImage alloc] initWithData:newData];
NSLog(@"%@",newImage);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end