我在此链接中使用了以下代码: How to resize an UIImage while maintaining its Aspect Ratio 通过维护其宽高比将图像大小调整为固定大小(100 * 100)。我在我的应用程序中使用 AsyncImageView 来使用url加载图像。您可以从以下链接获取 AsyncImageView 代码: nicklockwood / AsyncImageView
但这些图像看起来仍然像挤压一样。我该如何解决这个问题?
- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if( image.size.width > image.size.height ) {
scaleFactor = image.size.width / image.size.height;
scaledSize.width = newSize.width;
scaledSize.height = newSize.height / scaleFactor;
}
else {
scaleFactor = image.size.height / image.size.width;
scaledSize.height = newSize.height;
scaledSize.width = newSize.width / scaleFactor;
}
UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
这是在我的应用程序中加载图像的方式: