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

Saving UIGraphicsContext image as .png instead of .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?谢谢

尝试将图像数据转换为 PNG 数据表示,然后再转换回 PNG 图像,例如:

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