How to Resize Image in Swift

How to Resize image in Swift?

See my blog post, Resize image in swift and objective C, for further details.

Image resize function in swift as below.

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size

let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height

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

// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(origin: .zero, size: newSize)

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}

Use the above function and resize image with 200*200 as below code

self.resizeImage(UIImage(named: "yourImageName")!, targetSize: CGSizeMake(200.0, 200.0))

swift3 updated

 func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size

let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height

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

// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage!
}

How to resize Image with SwiftUI?

You should use .resizable() before applying any size modifications on an Image.

Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)

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

How to resize image in UIView?

Welcome.
You need to either give the imageView a frame or use Autolayout to set a layout for the image inside the UIView.

so either add this at the end:

imageView.frame = view.frame

But this will not be dynamic and you should learn how to keep updating the frame whenever the superview's frame changes.

Or you can add this, instead:

imageView.translatesAutoresizingMaskIntoConstraints = false
let constraints = [
imageView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
imageView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)

Anyway, I really recommend you read up about AutoLayout a bit before you continue:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/index.html
https://www.raywenderlich.com/811496-auto-layout-tutorial-in-ios-getting-started

If you find programmatic AutoLayout to be too challenging, I would recommend possibly starting with Storyboards first.

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()
}
}

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)

}


Related Topics



Leave a reply



Submit