将 UIButton 中的图像缩放到 AspectFit?

scale Image in an UIButton to AspectFit?(将 UIButton 中的图像缩放到 AspectFit?)
本文介绍了将 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)