将 UIButton 中的图像缩放到 AspectFit? [英] scale Image in an UIButton to AspectFit?

查看:13
本文介绍了将 UIButton 中的图像缩放到 AspectFit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像添加到 UIButton,并且还想缩放我的图像以适应 UIButton(使图像更小).请教我怎么做.

I want to add an image to a UIButton, and also want to scale my image to fit with the UIButton (make image smaller). Please show me how to do it.

这是我尝试过的,但它不起作用:

This is what I have tried, but it does't work:

  • 将图像添加到按钮并使用 setContentMode:
[self.itemImageButton setImage:stretchImage forState:UIControlStateNormal];
[self.itemImageButton setContentMode:UIViewContentModeScaleAspectFit];

  • 制作拉伸图像":
  • UIImage *stretchImage = [updatedItem.thumbnail stretchableImageWithLeftCapWidth:0 topCapHeight:0];
    

    推荐答案

    如果你真的想缩放一张图片,那就去做吧,但是你应该在使用它之前调整它的大小.在运行时调整它的大小只会丢失 CPU 周期.

    If you really want to scale an image, do it, but you should resize it before using it. Resizing it at run time will just lose CPU cycles.

    这是我用来缩放图像的类别:

    This is the category I'm using to scale an image :

    UIImage+Extra.h

    UIImage+Extra.h

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

    UIImage+Extra.m

    UIImage+Extra.m

    @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)) {
    
            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:
    
    UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0);
    
    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
    

    您可以根据需要使用它.喜欢:

    You can use it to the size you want. Like :

    [self.itemImageButton setImage:[stretchImage imageByScalingProportionallyToSize:CGSizeMake(20,20)]];
    

    这篇关于将 UIButton 中的图像缩放到 AspectFit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆