我是新来iPhone开发代码,我想搜索一些代码来调整我的UIImage到指定的大小,但保持比例。指定的大小就像一个图像不能跨越边界的框架,在该边界内图像应缩放以适合框架并保持比例,我目前使用的代码可以调整大小,但不能保留比率,只需粘贴在这里,看看我是否可以做一些微不足道的修改,使它成为可能。iPhone开发:任何片段来调整图像大小,并保持比例?
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
return newImage;
}
你可以通过设置UIImageView的属性来做到这一点。不知道如何处理UIImage本身。 – nhahtdh