如何等比例缩放UIImageView?

349

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

图片已经被缩放,但位置不在左上角。缩放图像/ imageView的最佳方法是什么,如何更正位置?


我有类似于你的代码,但对我不起作用。"UIImage *image = [UIImage imageNamed:imageString]; UIImageView *imageView = [[UIImage alloc] initWithImage:image];" 抛出了一个异常,导致我的应用程序崩溃,并显示如下错误信息:"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage initWithImage:]: unrecognized selector sent to instance 0xd815930'" - Spire
3
@Spire说的是UIImageView而不是UIImage ^^ - Thomas Decaux
2
这对我有效。记得将.contentMode设置为你的UIImageView,而不是UIImage。这是我的代码:UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)] - JBB
17个回答

7

对于Swift语言:

self.imageViews.contentMode = UIViewContentMode.ScaleToFill

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

3
如果这里提供的解决方案对您不起作用,而您的图像资产实际上是PDF格式,请注意XCode实际上将PDF与图像文件区别对待。特别是,它似乎无法正确缩放以填充PDF:最终结果是平铺。这让我很疯狂,直到我发现问题出在PDF格式上。将其转换为JPG格式,您就可以顺利进行了。

2
我使用了以下代码。其中,imageCoverView是一个包含UIImageView的UIView。
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;
}

1

通常我会在我的应用程序中使用这种方法(兼容Swift 2.x):

// 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
}

0

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

image.center = [[imageView window] center];

3
它不会缩放图像,只会将其居中。 - David Salzer

-3

以下是如何轻松缩放的方法。

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

UIImage *thumbnail = [originalImage _imageScaledToSize:CGSizeMake(40.0, 40.0) interpolationQuality:1];

你好,你知道interpolationQuality参数是做什么的吗?谢谢。 - user102008
27
这是一个未记录的方法(下划线已经透露了端倪),可能导致应用程序被拒绝。 - Shaggy Frog
插值质量参数是用来做什么的? - lostInTransit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接