2015-02-07 122 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

相关问题