gpt4 book ai didi

ios - 如何从 UIImageView 压缩图像?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:13 25 4
gpt4 key购买 nike

我正在使用来自 UIImageView * 图像的图像上传到服务器。我使用类似的代码NSData *imageToUpload = UIImageJPEGRepresentation(image.image, 90);但我想将图像压缩到小于 2MB。如何从 UIImageView 压缩图像?

最佳答案

这是你需要的一段代码

CGFloat compression = 222.0f;
CGFloat maxCompression = 202.1f;
int maxFileSize = 160*165; //fill your size need

NSData *imageDat = UIImageJPEGRepresentation(image.image, compression);

while ([imageDat length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1222;
imageDat = UIImageJPEGRepresentation(decodedimage.image, compression);
}
NSLog(@"image compressed success");

[image setImage:[UIImage imageWithData:imageDat]];

希望这对您有帮助!!!

关于ios - 如何从 UIImageView 压缩图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14949132/

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