I have been struggling resizing an image. Basically I have stumpled upon: How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?
This code uses UIGraphicsImageRenderer introduced in iOS 10: in my testing it was 10-40% faster than earlier 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
}
}
If you're dealing with PNG images that contain transparencies, then the accepted answer function will actually convert the transparent areas to black.
If you wish to scale and keep the transparencies in place, try this function:
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
}
}
Based on swift_dan's answer, an 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
}
Further improving on @rommex's answer using a 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))
}
}
}
If you are using kingfisher lib for loading images in you project and want to resize it here is the way:
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 the answer of @Christoph R
This is a continuation to @Christoph R 's answer posted 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!
}
at callers site
TaskUtilties.resizeImage(image: rawImage!, newWidth: CGFloat(50))