2015-02-07 119 views
0

我有這樣的代碼來調整圖片的大小爲縮略圖,:保存UIGraphicsContext圖像png格式,而不是.jpg文件

//MAKE THUMBNAIL 

    CGSize origImageSize = chosenImage.size; 

    //the rectangle of the thumbnail; 
    CGRect newRect = CGRectMake(0, 0, 70, 70); 

    //scaling ratio 
    float ratio = MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height); 


    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0); 


    CGRect projectRect; 
    projectRect.size.width= ratio*origImageSize.width; 
    projectRect.size.height=ratio*origImageSize.height; 
    //center the image 
    projectRect.origin.x= ((newRect.size.width-projectRect.size.width)/2); 
    projectRect.origin.y=((newRect.size.height-projectRect.size.height)/2); 
    [chosenImage drawInRect:projectRect]; 

    //get the image from the image context 
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 
    self.thumbnailView.image=smallImage; 

    UIImageWriteToSavedPhotosAlbum(smallImage, self, nil, nil); //20kb 
    UIGraphicsEndImageContext(); 

它保存圖像爲JPG,反正是有,我可以將其保存爲PNG代替?由於

回答

2

試試你的圖像數據轉換成PNG數據表示,然後回一個PNG圖像,例如:

UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 
NSData *pngImageData = UIImagePNGRepresentation(smallImage); 
UIImage *pngSmallImage = [UIImage imageWithData:pngImageData]; 
UIImageWriteToSavedPhotosAlbum(pngSmallImage, self, nil, nil); //20kb 
UIGraphicsEndImageContext(); 
+0

我會失去的圖像質量做這個?將圖像存儲爲PNG會更好嗎?還是沒有太大區別? – Kex 2015-02-07 19:09:25

+0

@Kex懷疑它。 PNG支持無損數據壓縮。 – 2015-02-07 20:11:20

+0

好的,謝謝你的幫助! – Kex 2015-02-08 11:59:49

相關問題