objective-c 如何按比例缩放 UIImageView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/185652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:49:12  来源:igfitidea点击:

How to scale a UIImageView proportionally?

objective-ccocoa-touch

提问by Ronnie Liew

I have a UIImageView and the objective is to scale it down proportionally by giving it either a height or width.

我有一个 UIImageView,目标是通过给它一个高度或宽度来按比例缩小它。

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

//Add image view
[self.view addSubview:imageView];   

//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;

//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;

The image did get resized but the position is not at the top left. What is the best approach to scaling image/imageView and how do I correct the position?

图像确实调整了大小,但位置不在左上角。缩放图像/图像视图的最佳方法是什么以及如何纠正位置?

回答by Ken Abrams

Fixed easily, once I found the documentation!

找到文档后,轻松修复!

 imageView.contentMode = UIViewContentModeScaleAspectFit;

回答by Hymansonkr

I've seen a bit of conversation about scale types so I decided to put together an article regarding some of the most popular content mode scaling types.

我看过一些关于缩放类型的讨论,所以我决定整理一篇关于一些最流行的内容模式缩放类型的文章

The associated image is here:

相关图片在这里:

enter image description here

在此处输入图片说明

回答by Jane Sales

I just tried this, and UIImage does not support _imageScaledToSize.

我刚试过这个,UIImage 不支持 _imageScaledToSize。

I ended up adding a method to UIImage using a category - a suggestion I found on the Apple Dev forums.

我最终使用类别向 UIImage 添加了一个方法 - 我在 Apple Dev 论坛上找到的建议。

In a project-wide .h -

在项目范围的 .h -

@interface UIImage (Extras)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;
@end;

Implementation:

执行:

@implementation UIImage (Extras)

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {

    UIImage *sourceImage = self;
    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor < heightFactor) 
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image

        if (widthFactor < heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        } else if (widthFactor > heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }


    // this is actually the interesting part:

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if(newImage == nil) NSLog(@"could not scale image");


    return newImage ;
}

@end;

回答by Li-chih Wu

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;

回答by Chris Lundie

You could try making the imageViewsize match the image. The following code is not tested.

您可以尝试使imageView大小与image. 以下代码未经测试。

CGSize kMaxImageViewSize = {.width = 100, .height = 100};
CGSize imageSize = image.size;
CGFloat aspectRatio = imageSize.width / imageSize.height;
CGRect frame = imageView.frame;
if (kMaxImageViewSize.width / aspectRatio <= kMaxImageViewSize.height) 
{
    frame.size.width = kMaxImageViewSize.width;
    frame.size.height = frame.size.width / aspectRatio;
} 
else 
{
    frame.size.height = kMaxImageViewSize.height;
    frame.size.width = frame.size.height * aspectRatio;
}
imageView.frame = frame;

回答by neoneye

one can resize an UIImage this way

可以通过这种方式调整 UIImage 的大小

image = [UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp];

回答by Nate Flink

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 


//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;

//change width of frame
//CGRect frame = imageView.frame;
//frame.size.width = 100;
//imageView.frame = frame;

//original lines that deal with frame commented out, yo.
imageView.frame = CGRectMake(10, 20, 60, 60);

...

//Add image view
[myView addSubview:imageView]; 

The original code posted at the top worked well for me in iOS 4.2.

顶部发布的原始代码在 iOS 4.2 中对我来说效果很好。

I found that creating a CGRect and specifying all the top, left, width, and height values was the easiest way to adjust the position in my case, which was using a UIImageView inside a table cell. (Still need to add code to release objects)

我发现创建一个 CGRect 并指定所有的顶部、左侧、宽度和高度值是在我的情况下调整位置的最简单方法,这是在表格单元格内使用 UIImageView 。(仍然需要添加代码来释放对象)

回答by Jeffrey Neo

Set your ImageView by selecting Mode to Aspect Filland check the Clip Subviewsbox.

通过选择模式设置您的 ImageViewAspect Fill并选中该Clip Subviews框。

enter image description here

在此处输入图片说明

回答by vvamondes

This works fine for me Swift 2.x:

这对我来说很好用 Swift 2.x:

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true;

回答by P.J.Radadiya

Set your UIimageviewby scale.........

UIimageview按比例设置您的…………

enter image description here

在此处输入图片说明