简体   繁体   English

如何在保持其纵横比的同时调整AsyncImageView(UIImageView的子类)的大小

[英]How to resize AsyncImageView (subclass of UIImageView) while maintaining its Aspect Ratio

I have used the below code from this link: How to resize an UIImage while maintaining its Aspect Ratio to resize an image to fixed size (100*100) by maintaing its aspect ratio. 我从此链接使用了以下代码: 如何在保持UI纵横比的同时调整UIImage的大小,以通过保持UI纵横比将图像调整为固定大小(100 * 100)。 I have used AsyncImageView in my application to load the images using url's. 我在应用程序中使用AsyncImageView来使用url加载图像。 You can get the AsyncImageView code from this link: nicklockwood / AsyncImageView 您可以从以下链接获取AsyncImageView代码: nicklockwood / AsyncImageView

But still those images looks like squeezed. 但是,那些图像看起来仍然像是被压缩了。 How can I solve this issue? 我该如何解决这个问题?

- (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;
}

This is how the images are being loaded in my application: 这是在我的应用程序中加载图像的方式: 在此处输入图片说明

Why not use SDWebImage to get remote image? 为什么不使用SDWebImage获取远程图像? Here is the link: https://github.com/rs/SDWebImage 这是链接: https : //github.com/rs/SDWebImage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM