代码之家  ›  专栏  ›  技术社区  ›  Ronnie Liew

如何按比例缩放UIImageView?

  •  335
  • Ronnie Liew  · 技术社区  · 16 年前

    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;
    

    17 回复  |  直到 9 年前
        1
  •  541
  •   Srikar Appalaraju Tonetel    13 年前

    很容易修复,一旦我找到文件!

     imageView.contentMode = UIViewContentModeScaleAspectFit;
    
        2
  •  401
  •   Jacksonkr    8 年前

    我看过一些关于音阶类型的对话,所以我决定把 article regarding some of the most popular content mode scaling types .

    enter image description here

        3
  •  78
  •   john.k.doe    11 年前

    我刚试过,UIImage不支持imageScaledToSize。

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

    在整个项目中。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;
    
        4
  •  38
  •   Li-chih Wu    12 年前
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    
        5
  •  30
  •   P.J.Radadiya Yevgen THD    9 年前

    你可以试着做 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;
    
        6
  •  13
  •   neoneye    13 年前

    可以这样调整UIImage的大小

    image = [UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp];
    
        7
  •  13
  •   Nate Flink    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]; 
    

    我发现在我的例子中,创建CGRect并指定所有的top、left、width和height值是调整位置的最简单方法,这是在表单元格中使用UIImageView。(仍需要添加代码以释放对象)

        8
  •  13
  •   Jeffrey Neo    10 年前

    Aspect Fill 并检查 Clip Subviews

    enter image description here

        9
  •  10
  •   vvamondes    8 年前

    imageView.contentMode = .ScaleAspectFill
    imageView.clipsToBounds = true;
    
        10
  •  9
  •   P.J.Radadiya Yevgen THD    8 年前

    设置您的 UIimageview

    enter image description here

        11
  •  6
  •   Somir Saikia    9 年前

    对于Swift:

    self.imageViews.contentMode = UIViewContentMode.ScaleToFill
    
        12
  •  5
  •   Peter Kreinz    11 年前

    #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
    
        13
  •  2
  •   P.J.Radadiya Yevgen THD    9 年前

    我用的是代码.whereimageCoverView是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;
    }
    
        14
  •  2
  •   Lane Rettig    8 年前

    如果这里提出的解决方案对您不起作用,并且您的图像资产实际上是一个PDF,请注意XCode实际上对待PDF的方式与对待图像文件的方式不同。尤其是,它似乎无法按比例正确填充PDF:而是以平铺结束。这让我抓狂,直到我发现问题是PDF格式。转换成JPG,你应该很好去。

        15
  •  1
  •   Alessandro Ornano    9 年前

    通常我对我的应用程序使用这种方法( 斯威夫特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
    }
    
        16
  •  0
  •   CoolBeans Jake    13 年前

    image.center = [[imageView window] center];
    
        17
  •  -3
  •   kdbdallas    16 年前

    这是你如何轻松地缩放它的方法。

    它可以在2.x中与模拟器和iPhone一起工作。

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