gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:33:20 25 4
gpt4 key购买 nike

我有这段代码可以将我的图像调整为缩略图:

//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();

关于ios - 将 UIGraphicsContext 图像保存为 .png 而不是 .jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com