当前位置:  开发笔记 > IOS > 正文

如何按比例缩放UIImageView?

如何解决《如何按比例缩放UIImageView?》经验,为你挑选了12个好方法。

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

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



1> 小智..:

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

 imageView.contentMode = UIViewContentModeScaleAspectFit;


你究竟回答了什么?在罗尼的问题中,他提到他使用它
您的imageView可能没有裁剪.试试imageView.layer.masksToBounds = YES;

2> Jacksonkr..:

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

相关图片如下:

在此输入图像描述



3> Jane Sales..:

我刚试过这个,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;



4> Li-chih Wu..:
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;



5> Chris Lundie..:

您可以尝试使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> neoneye..:

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

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



7> 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]; 

发布在顶部的原始代码在iOS 4.2中适用于我.

我发现创建一个CGRect并指定所有top,left,width和height值是在我的情况下调整位置的最简单方法,即在表格单元格中使用UIImageView.(仍然需要添加代码来释放对象)



8> Jeffrey Neo..:

选择Mode to设置ImageView Aspect Fill并选中Clip Subviews复选框.

在此输入图像描述



9> 小智..:

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

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



10> P.J.Radadiya..:

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

在此输入图像描述



11> Somir Saikia..:

对于Swift:

self.imageViews.contentMode = UIViewContentMode.ScaleToFill



12> Peter Kreinz..:

的UIImageView + Scale.h:

#import 

@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

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有