前段时间在做项目的时候遇到图片保存到沙盒中尺寸变大问题???
尺寸变大?为什么之前没有注意到?
虽然用其他方法给暂时解决了问题,但是一直存有疑惑。今天闲来无事,把之前的问题重新拿出来探究一下。
第一步,将一张图片直接保存到沙盒中进行测试。
[UIImageJPEGRepresentation(image, 1) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/1.jpg"] atomically:YES];
[UIImageJPEGRepresentation(image, 0.6) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/2.jpg"] atomically:YES];
[UIImagePNGRepresentation(image) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/3.png"] atomically:YES];
发现在沙盒中三张图片的尺寸都没有改变。
第二步,探索问题。
因为我之前是将图片重新绘制,然后试用writeToFile方法保存图片到沙盒的,有可能是因为流程上的问题?之所以说是流程上的问题,是因为之前重新绘制的图片拿出来后查看了图片的尺寸并没有变大,但是保存的时候就会变大。
// 4 方式
UIGraphicsBeginImageContextWithOptions(image.size, NO, 1);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image4 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image4 Name:@"4"];
// 5 方式
UIGraphicsBeginImageContextWithOptions(image.size, YES, 1);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image5 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image5 Name:@"5"];
// 6 方式 -> 图片尺寸会变大
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image6 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image6 Name:@"6"];
// 7 方式 -> 图片尺寸会变大
UIGraphicsBeginImageContextWithOptions(image.size, YES, 0);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image7 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image7 Name:@"7"];
// 8 方式
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image8 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image8 Name:@"8"];
- (void)writeImageWithImage:(UIImage *)image Name:(NSString *)imagename {
[UIImageJPEGRepresentation(image, 1) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.jpg",imagename]] atomically:YES];
[UIImageJPEGRepresentation(image, 0.6) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@_2.jpg",imagename]] atomically:YES];
[UIImagePNGRepresentation(image) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.png",imagename]] atomically:YES];
}
经过测试发现,方式6和方式7会使图片变大,变大的比例和UIScreen的scale一致。他们之间的共性就是UIGraphicsBeginImageContextWithOptions方法的第三个参数传入的值都为0.猜想有可能是因为我们设置为0时,它会按照UIScreen的scale来进行绘制。
进一步猜想,这个UIGraphicsBeginImageContextWithOptions的scale会造成什么样的后果呢?
// 9 方式
UIGraphicsBeginImageContextWithOptions(image.size, YES, 0.5);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image9 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image9 Name:@"9"];
// 10 方式
UIGraphicsBeginImageContextWithOptions(image.size, YES, 2);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *image10 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self writeImageWithImage:image10 Name:@"10"];
经过测试,image.size打印是原始尺寸,写入沙盒的是根据scale改变后的尺寸,重新从沙盒中加载的image尺寸也是改变后的尺寸。
具体底层是什么原因造成这样的现象目前还不清楚,如果有明白的,欢迎帮忙解惑。
网友评论