gpt4 book ai didi

iphone - 调整图像大小而不损失其质量

转载 作者:行者123 更新时间:2023-12-03 20:30:44 25 4
gpt4 key购买 nike

当我将导入图像[其他图像]发送到服务器时,花费的时间比 iPhone 图像[通过默认相机拍照]要少。是否有选项可以在不损失质量的情况下调整图像大小?

最佳答案

使用此代码会有帮助

+ (UIImage *)scaleImage:(UIImage *)image maxWidth:(int) maxWidth maxHeight:(int) maxHeight
{
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);

if (width <= maxWidth && height <= maxHeight)
{
return image;
}

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);

if (width > maxWidth || height > maxHeight)
{
CGFloat ratio = width/height;

if (ratio > 1)
{
bounds.size.width = maxWidth;
bounds.size.height = bounds.size.width / ratio;
}
else
{
bounds.size.height = maxHeight;
bounds.size.width = bounds.size.height * ratio;
}
}

CGFloat scaleRatio = bounds.size.width / width;
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);

UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return imageCopy;

}

关于iphone - 调整图像大小而不损失其质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495843/

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