当我将导入图像[其他图像]发送到服务器所花费的时间少于iphone图像时[通过默认相机拍照]。是否可以选择调整图像大小而不会降低图像质量?
答案 0 :(得分:3)
使用此代码可以提供帮助
+ (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;
}
答案 1 :(得分:2)
试试这个波纹管方法......
像下面一样打电话......
UIImage *imgBig = [self imageWithImage:yourImage scaledToSize::yourNewImageSize];
并使用这个波纹管方法..
-(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContextWithOptions(newSize, YES, image.scale);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}