iOS 圖片保存到沙盒中尺寸變大問題

前段時(shí)間在做項(xiàng)目的時(shí)候遇到圖片保存到沙盒中尺寸變大問題???
尺寸變大?為什么之前沒有注意到?
雖然用其他方法給暫時(shí)解決了問題,但是一直存有疑惑。今天閑來無事,把之前的問題重新拿出來探究一下。

第一步,將一張圖片直接保存到沙盒中進(jìn)行測試。

[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];

發(fā)現(xiàn)在沙盒中三張圖片的尺寸都沒有改變。

第二步,探索問題。
因?yàn)槲抑笆菍D片重新繪制,然后試用writeToFile方法保存圖片到沙盒的,有可能是因?yàn)榱鞒躺系膯栴}?之所以說是流程上的問題,是因?yàn)橹爸匦吕L制的圖片拿出來后查看了圖片的尺寸并沒有變大,但是保存的時(shí)候就會變大。

// 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];
}

經(jīng)過測試發(fā)現(xiàn),方式6和方式7會使圖片變大,變大的比例和UIScreen的scale一致。他們之間的共性就是UIGraphicsBeginImageContextWithOptions方法的第三個(gè)參數(shù)傳入的值都為0.猜想有可能是因?yàn)槲覀冊O(shè)置為0時(shí),它會按照UIScreen的scale來進(jìn)行繪制。

進(jìn)一步猜想,這個(gè)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"];

經(jīng)過測試,image.size打印是原始尺寸,寫入沙盒的是根據(jù)scale改變后的尺寸,重新從沙盒中加載的image尺寸也是改變后的尺寸。
具體底層是什么原因造成這樣的現(xiàn)象目前還不清楚,如果有明白的,歡迎幫忙解惑。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容