您可以缩放图像以适应图像视图的宽度。
您可以使用一个类别UIImage
来创建具有选定宽度的新图像。
@interface UIImage (Scale)
-(UIImage *)scaleToWidth:(CGFloat)width;
@end
@implementation UIImage (Scale)
-(UIImage *)scaleToWidth:(CGFloat)width
{
UIImage *scaledImage = self;
if (self.size.width != width) {
CGFloat height = floorf(self.size.height * (width / self.size.width));
CGSize size = CGSizeMake(width, height)
// Create an image context
UIGraphicsBeginImageContext(size);
// Draw the scaled image
[self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
// Create a new image from context
scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// Pop the current context from the stack
UIGraphicsEndImageContext();
}
// Return the new scaled image
return scaledImage;
}
@end
这样您就可以使用它来缩放图像
UIImage *scaledImage = [originalImage scaleToWidth:myImageView.frame.size.width];
myImageView.contentMode = UIViewContentModeTopLeft;
myImageView.image = scaledImage;