将UIGraphicsContext图像保存为.png而不是.jpg

时间:2015-02-07 19:02:11

标签: ios xcode uiimage uigraphicscontext

我有这个代码将我的图像调整为缩略图:

//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吗?感谢

1 个答案:

答案 0 :(得分:2)

尝试将图片的数据转换为PNG数据,然后再转换回PNG图片,例如:

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