Scale Image in an Uibutton to Aspectfit

scale Image in an UIButton to AspectFit?

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

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

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 won't go to Aspect Fit in iPhone

I've had that problem before. I solved it by putting my image in a UIImageView, where contentMode settings actually work, and putting a transparent custom UIButton over top of that.

EDIT: This answer is obsolete. See @Werner Altewischer's answer for the correct answer in modern versions of iOS.

I can't fit an image in a UIButton in Swift 5

I solved the problem, it's the same as here : https://stackoverflow.com/a/57905360/1952656

But we can't give the same answer to two differents question, so go check directly

AspectFit image inside an UIButton

I found the solution !!

You need to add

cameraUIButton.imageView?.adjustsImageSizeForAccessibilityContentSizeCategory = true

So the full code to aspectFit a vector image inside a button is :

cameraUIButton.imageView?.contentMode = .scaleAspectFit
cameraUIButton.imageView?.adjustsImageSizeForAccessibilityContentSizeCategory = true

I don't know why it's not trueby default as it's turned to it if you click on the button or if you animate it.

Make UIButton Image fit into the Button

I'm assuming you are using Xcode 13 Storyboard designer.

If so, change the Button Style from "Plain":

Sample Image

to "Default":

Sample Image

Now your image will fit to the constrained button size.

Which content mode to use for scaling an image in an UIButton?

I want the images to keep their original size, shape & quality. So I guess I want to use center and/or aspect fit for content mode. Is this the correct mode?

If you want button to change it's frame according to image then you should get image.size and update the frame of the button.

But aspect fit is especially needed, when an image is bigger then the UIButton, right?

Right!

What if the image is a fraction bigger then the [100 width / 75 height] rectangle, and I use aspect fit for content mode. This will 'scale down' the image to the size of the UIButton, right?

Right!

Will the quality of the image decrease then? If yes, can this be a big decrease, or just marginal?

Quality of image decrease when you scale up a Image. So Scaling down does;t make a big impact on image (if scaled in ratio).



Related Topics



Leave a reply



Submit