The Simplest Way to Resize an Uiimage

The simplest way to resize an UIImage?

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options.

Or you can use this utility method, if you actually need to resize an image:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Example usage:

#import "MYUtil.h"

UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];

UIImage resizing scale

For Swift 4 And Swift 3 use below UIImage extension for resizing image. It's calculated height according to given width.

extension UIImage {
func resized(toWidth width: CGFloat) -> UIImage? {
let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
defer { UIGraphicsEndImageContext() }
draw(in: CGRect(origin: .zero, size: canvasSize))
return UIGraphicsGetImageFromCurrentImageContext()
}
}

resizing UIImage the fastest and efficient way

In a performance sample of Image Resizing Techniques the score was

  • 0.1420 UIKit
  • 0.1722 Core Graphics
  • 0.1616 Image I/O
  • 2.4983 Core Image
  • 2.3126 vImage

so the fastest and simplest way is:


let renderer = UIGraphicsImageRenderer(size: size)
let resized = renderer.image { (context) in
image.draw(in: CGRect(origin: .zero, size: size))
}

Resize a UIImage the right way is from 2009 but contains useful talk about interpolation quality and preserving the aspect ratio.

Resize UIImage to 200x200pt/px

Here is my code. The Image is in width 850 px and not 200 px:

 func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {

let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}


@IBAction func chooseImage(sender: AnyObject) {


var myPickerController = UIImagePickerController()
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
myPickerController.delegate = self;
self.presentViewController(myPickerController, animated: true, completion: nil)


}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])

{
var imagenow = info[UIImagePickerControllerOriginalImage] as? UIImage

imageImage.image = resizeImage(imagenow!, newWidth: 200)



pimg2 = imageImage.image!

cidnew2 = textFieldCID!.text!
pname2 = textFieldName!.text
pmanu2 = textFieldMan!.text
pnick2 = textFieldNick!.text
podate2 = textFieldPODate!.text
pno2 = textFieldArtNo!.text



self.dismissViewControllerAnimated(true, completion: nil)

}

Simple resizing of UIImage in XCODE

This may be overkill but, you can simply take your image, and create a graphics context at that resolution you want, then you can set the tempImage as the UIImageView, overwriting it.

        UIImage *image = YourImageView.image;
UIImage *tempImage = nil;
CGSize targetSize = CGSizeMake(80,60);
UIGraphicsBeginImageContext(targetSize);

CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
thumbnailRect.origin = CGPointMake(0.0,0.0);
thumbnailRect.size.width = targetSize.width;
thumbnailRect.size.height = targetSize.height;

[image drawInRect:thumbnailRect];

tempImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

YourImageView.image = tempImage;

How to resize an UIImage based on device size and scale?

Since the scale factor is specified in UIGraphicsBeginImageContextWithOptions, it is not needed to multiply the width and height by the screen scale when scaledRect is calculated.

And If you specify a value of 0.0 in UIGraphicsBeginImageContextWithOptions, the scale factor is automatically set to the scale factor of the device’s main screen.

- (UIImage *)hResizeImage:(UIImage *)image withTargetSize:(CGSize)targetSize {
CGSize size = image.size;
CGFloat widthRatio = targetSize.width / image.size.width;
CGFloat heightRatio = targetSize.height / image.size.height;

// Figure out what our orientation is, and use that to form the rectangle
CGSize newSize;
if(widthRatio > heightRatio) {
newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio);
} else {
newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio);
}

// This is the rect that we've calculated out and this is what is actually used below
CGRect scaledRect = CGRectMake(0, 0, newSize.width, newSize.height);

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(scaledRect.size, false, 0.0f);
[image drawInRect:scaledRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Resize UIImage by keeping Aspect ratio and width

The method of Srikar works very well, if you know both height and width of your new Size.
If you for example know only the width you want to scale to and don't care about the height you first have to calculate the scale factor of the height.

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width
{
float oldWidth = sourceImage.size.width;
float scaleFactor = i_width / oldWidth;

float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;

UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}


Related Topics



Leave a reply



Submit