Resize UIImage to 200x200pt / px

I struggled to resize the image. Basically I came across: How to reduce UIImage and make it clear / sharp at the same time, not blurry?

This seems like the right solution, but for some reason it doesn't work correctly.

My application works with photos from the Roll camera. The size of these photos should be approximately 200x200, while width is important, not height.

Unfortunately, I do not have a sample code, because I abandoned it in my anger due to a non-working solution, sorry.

+78
ios swift uiimage
Aug 12 '15 at 13:37
source share
13 answers

Here is my code. The image has a width of 850 pixels, 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) } 
+144
Aug 13 '15 at 9:17
source share

Based on swift_dan answer, update for Swift 3

 func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage? { let scale = newWidth / image.size.width let newHeight = image.size.height * scale UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } 
+61
01 Oct '16 at 18:06
source share

If you are dealing with PNG images that contain transparencies, then the accepted answer function will actually convert the transparent areas to black.

If you want to scale and hold transparencies in place, try this feature:

SWIFT 4

 extension UIImage { func scaleImage(toSize newSize: CGSize) -> UIImage? { var newImage: UIImage? let newRect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height).integral UIGraphicsBeginImageContextWithOptions(newSize, false, 0) if let context = UIGraphicsGetCurrentContext(), let cgImage = self.cgImage { context.interpolationQuality = .high let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: newSize.height) context.concatenate(flipVertical) context.draw(cgImage, in: newRect) if let img = context.makeImage() { newImage = UIImage(cgImage: img) } UIGraphicsEndImageContext() } return newImage } } 
+40
Jan 04 '16 at 20:24
source share

For Swift 3.0

just add this snippet as an extension in UIImage . However, remember that this will not make the image in square form, but if it was in this form, the result will be square.

 extension UIImage { func resizeImage(newWidth: CGFloat) -> UIImage { let scale = newWidth / self.size.width let newHeight = self.size.height * scale UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) self.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } } 
+21
Dec 05 '16 at 11:11
source share

Swift 4.0 -

If you are dealing with images that contain transparencies, then the accepted answer function will actually convert the transparent areas to black.

If you want to scale and hold transparencies, try this feature:

 func resizeImageWith(image: UIImage, newSize: CGSize) -> UIImage { let horizontalRatio = newSize.width / image.size.width let verticalRatio = newSize.height / image.size.height let ratio = max(horizontalRatio, verticalRatio) let newSize = CGSize(width: image.size.width * ratio, height: image.size.height * ratio) var newImage: UIImage if #available(iOS 10.0, *) { let renderFormat = UIGraphicsImageRendererFormat.default() renderFormat.opaque = false let renderer = UIGraphicsImageRenderer(size: CGSize(width: newSize.width, height: newSize.height), format: renderFormat) newImage = renderer.image { (context) in image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) } } else { UIGraphicsBeginImageContextWithOptions(CGSize(width: newSize.width, height: newSize.height), isOpaque, 0) image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) newImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() } return newImage } 
+10
Aug 29 '17 at 10:32 on
source share

This function will return an image with a width :

 func scaleImage(image: UIImage, maximumWidth: CGFloat) -> UIImage { let rect: CGRect = CGRectMake(0, 0, image.size.width, image.size.height) let cgImage: CGImageRef = CGImageCreateWithImageInRect(image.CGImage!, rect)! return UIImage(CGImage: cgImage, scale: image.size.width / maximumWidth, orientation: image.imageOrientation) } 

Swift 3.0

 func scaledImage(_ image: UIImage, maximumWidth: CGFloat) -> UIImage { let rect: CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height) let cgImage: CGImage = image.cgImage!.cropping(to: rect)! return UIImage(cgImage: cgImage, scale: image.size.width / maximumWidth, orientation: image.imageOrientation) } 
+7
Jul 07 '16 at 8:46
source share

This code uses the UIGraphicsImageRenderer introduced in iOS 10: in my testing it was 10-40% faster than in the previous samples with UIGraphicsBeginImageContext (Swift 4 / Xcode 9):

 extension UIImage { func renderResizedImage (newWidth: CGFloat) -> UIImage { let scale = newWidth / self.size.width let newHeight = self.size.height * scale let newSize = CGSize(width: newWidth, height: newHeight) let renderer = UIGraphicsImageRenderer(size: newSize) let image = renderer.image { (context) in self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize)) } return image } } 
+7
Dec 31 '17 at 8:38
source share

If you are using a zimfisher zip file to upload images in a project and want to resize it, here is how to do it:

Xcode 8 Swift 3x
 let imageUrl = URL(string: "your image url") //Size refer to the size which you want to resize your original image let size = CGSize(width: 60, height: 60) let processImage = ResizingImageProcessor(targetSize: size, contentMode: .aspectFit) cell.courseTitleImage.kf.setImage(with: imageUrl! , placeholder: UIImage(named: "placeholder"), options: [.transition(ImageTransition.fade(1)), .processor(processImage)], progressBlock: nil, completionHandler: nil) 

OR

Resize local image: - you can refer to @Christoph R's answer

+2
May 02 '17 at 8:11 a.m.
source share

Further improving @rommex's answer using maximum size in Swift 4.2:

 private extension UIImage { func scaled(to maxSize: CGFloat) -> UIImage? { let aspectRatio: CGFloat = min(maxSize / size.width, maxSize / size.height) let newSize = CGSize(width: size.width * aspectRatio, height: size.height * aspectRatio) let renderer = UIGraphicsImageRenderer(size: newSize) return renderer.image { context in draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize)) } } } 
+2
Jan 26 '19 at 16:25
source share
 func getScaledDimension(width: CGFloat, height: CGFloat,new_width: CGFloat, new_height: CGFloat)->CGPoint { let widthAspect = (width / new_width) let heightAspect = (height / new_height) if widthAspect == 0 || heightAspect == 0 { return CGPoint(x: width, y: height) } var width1 : CGFloat = 0 var height1 : CGFloat = 0 if widthAspect > heightAspect { width1 = (width) / heightAspect height1 = (height) / heightAspect } else { width1 = (width) / widthAspect height1 = (height) / widthAspect } return CGPoint(x: width1, y: height1 ) } func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage { let rect = CGRectMake(0, 0, targetSize.width, targetSize.height) UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0) image.drawInRect(rect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } let imagesize = getScaledDimension(image.size.width, height: image.size.height , new_width: Width, new_height: Hieght) print("Image Size Scaled Dimension -> H:\(imagesize.x) W:\(imagesize.y)") let newImage = ResizeImage(image, targetSize: CGSizeMake(imagesize.x,imagesize.y)) print("Resize Image Size -> H\(newImage.size.height) W\(newImage.size.width) ") 
+1
Jul 25 '16 at 9:39
source share

By reducing the image size by 1024, you can always convert according to the capacity of the server

 func resizeImage(image: UIImage) -> UIImage { if image.size.height >= 1024 && image.size.width >= 1024 { UIGraphicsBeginImageContext(CGSize(width:1024, height:1024)) image.draw(in: CGRect(x:0, y:0, width:1024, height:1024)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } else if image.size.height >= 1024 && image.size.width < 1024 { UIGraphicsBeginImageContext(CGSize(width:image.size.width, height:1024)) image.draw(in: CGRect(x:0, y:0, width:image.size.width, height:1024)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } else if image.size.width >= 1024 && image.size.height < 1024 { UIGraphicsBeginImageContext(CGSize(width:1024, height:image.size.height)) image.draw(in: CGRect(x:0, y:0, width:1024, height:image.size.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } else { return image } } 
0
Jun 28 '17 at 12:59 on
source share

This is a follow-up to @Christoph R's answer published for Swift 3.0. This code works for Swift 5.0.1.

 static func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage { let scale = newWidth / image.size.width let newHeight = image.size.height * scale UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } 

on the site of subscribers

 TaskUtilties.resizeImage(image: rawImage!, newWidth: CGFloat(50)) 
0
Aug 29 '19 at 18:15
source share

This code works fine on a square image and does not lose quality

 extension UIImage { func resize(targetSize: CGSize) -> UIImage { return UIGraphicsImageRenderer(size:targetSize).image { _ in self.draw(in: CGRect(origin: .zero, size: targetSize)) } } } 

Answer from: Resizing an image without losing quality

0
Sep 30 '19 at 10:22
source share



All Articles