2016-03-14 32 views
0

我有以下代碼:爲什麼在轉換到base64和從base64轉換時,圖像的大小是原來的四倍?

UIImage *picture = [self scaledPicture:[APP getObject:@"pictures"][index]]; 
NSLog(@"picture size: %fx%f", picture.size.width, picture.size.height); 

NSString* imageData = [UIImageJPEGRepresentation(picture, 1.0f) base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]; 
NSLog(@"imageData size: %d", [imageData length]); 

NSString* percentEscapedPicture = [imageData stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; 
NSLog(@"percentEscapedPicture size: %d", [percentEscapedPicture length]); 

NSString* base64Picture = [NSString stringWithFormat:@"data:;base64,%@", percentEscapedPicture]; 
NSLog(@"base64Picture size: %d", [base64Picture length]); 

NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:base64Picture]]; 
NSLog(@"data size: %d", [data length]); 

UIImage* image = [UIImage imageWithData:data scale:1.0f]; 
NSLog(@"image size: %fx%f", image.size.width, image.size.height); 

這裏有日誌結果:

picture size: 765.000000x1024.000000 
imageData size: 3584062 
percentEscapedPicture size: 3958522 
base64Picture size: 3958535 
data size: 2619123 
image size: 1530.000000x2048.000000 

正如你所看到的,在兩個維度通過因子2圖像的分辨率提高。我不明白爲什麼。

我需要這個(在編碼和解碼之間的幾個額外的步驟)爲我的應用程序。只要我記得,我在我的應用程序中使用了相同的base64編碼/解碼代碼。據我所知,base64增加了數據的大小約33%,但爲什麼分辨率會增加?

+0

看起來你的原始圖像是一個視網膜圖像,即它有一個1530 x 2048像素的分辨率和一個比例因子2.因此它假裝的大小是765 x 1024.您還想輸出比例因子: 'NSLog(@「picture size:%fx%f scale:%f」,picture.size.width,picture.size.height,picture.scale);' – Codo

+0

__1。)__您正在打印圖像的_point_大小隻是,你的原始圖像可能是視網膜,使用比例因子'2.0',__2。)__在你從圖像中重新加載圖像後,你用'1.0'來縮放圖像,這意味着圖像的大小增加了一倍。 – holex

回答

1

原始圖像的scale屬性設置爲2。當您將它作爲純數據以四捨五入的方式轉換爲基數64並返回時,您將該信息丟棄。

+0

另請參閱我的答案在這裏:http://stackoverflow.com/questions/35945493/why-is-my-cgimage-3-x-the-size-of-the-uiimage/35945544#35945544其實你的問題是有效的重複。 – matt

+0

非常感謝,我嘗試將比例設置爲1.0f,但我沒有考慮視網膜 – Kevin