346

我有一个 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;

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

4

17 回答 17

543

一旦我找到文档,就可以轻松修复!

 imageView.contentMode = .scaleAspectFit
于 2010-02-20T00:29:24.490 回答
403

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

相关图像在这里:

在此处输入图像描述

于 2013-01-08T17:22:43.290 回答
78

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

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

在项目范围内的 .h -

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

执行:

@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;
于 2009-02-11T16:44:09.903 回答
39
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
于 2012-12-11T07:16:54.993 回答
30

您可以尝试使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;
于 2008-10-09T04:10:13.390 回答
13
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]; 

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

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

于 2011-06-15T18:10:36.373 回答
13

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

image = [UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp];
于 2011-05-25T14:55:59.300 回答
13

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

在此处输入图像描述

于 2015-04-03T03:31:22.850 回答
13

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

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true;
于 2016-10-07T16:13:49.187 回答
11

UIimageview按比例设置......

在此处输入图像描述

于 2016-12-16T09:37:32.547 回答
7

对于斯威夫特:

self.imageViews.contentMode = UIViewContentMode.ScaleToFill
于 2015-09-17T09:33:38.347 回答
5

UIImageView+Scale.h:

#import <Foundation/Foundation.h>

@interface UIImageView (Scale)

-(void) scaleAspectFit:(CGFloat) scaleFactor;

@end

UIImageView+Scale.m:

#import "UIImageView+Scale.h"

@implementation UIImageView (Scale)


-(void) scaleAspectFit:(CGFloat) scaleFactor{

    self.contentScaleFactor = scaleFactor;
    self.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);

    CGRect newRect = self.frame;
    newRect.origin.x = 0;
    newRect.origin.y = 0;
    self.frame = newRect;
}

@end
于 2013-11-20T16:22:12.667 回答
2

如果此处提出的解决方案不适合您,并且您的图像资产实际上是 PDF,请注意 XCode 实际上处理 PDF 与图像文件不同。特别是,它似乎无法缩放以正确填充 PDF:它最终被平铺。这让我发疯,直到我发现问题出在 PDF 格式上。转换为JPG,你应该很高兴。

于 2017-01-05T01:04:54.673 回答
2

我使用了以下代码。其中 imageCoverView 是 UIView 包含 UIImageView

if (image.size.height<self.imageCoverView.bounds.size.height && image.size.width<self.imageCoverView.bounds.size.width)
{
    [self.profileImageView sizeToFit];
    self.profileImageView.contentMode =UIViewContentModeCenter
}
else
{
    self.profileImageView.contentMode =UIViewContentModeScaleAspectFit;
}
于 2016-02-25T08:08:03.343 回答
1

Usually I use this method for my apps (Swift 2.x compatible):

// Resize UIImage
func resizeImage(image:UIImage, scaleX:CGFloat,scaleY:CGFloat) ->UIImage {
    let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(scaleX, scaleY))
    let hasAlpha = true
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
    image.drawInRect(CGRect(origin: CGPointZero, size: size))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return scaledImage
}
于 2016-02-25T08:17:15.513 回答
0

我认为你可以做类似的事情

image.center = [[imageView window] center];
于 2008-10-09T03:04:24.693 回答
-3

这是您可以轻松扩展它的方法。

这适用于模拟器和 iPhone 的 2.x。

UIImage *thumbnail = [originalImage _imageScaledToSize:CGSizeMake(40.0, 40.0) interpolationQuality:1];
于 2008-10-10T22:17:54.680 回答