-
-
Save haikieu/45e045a0a582249fc492e858ab57a301 to your computer and use it in GitHub Desktop.
Resize Image using vImage (Updated for Swift 5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
func resize(size: CGSize) -> UIImage? { | |
guard let cgImage = self.cgImage else { return nil} | |
var format = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 32, colorSpace: nil, | |
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.first.rawValue), | |
version: 0, decode: nil, renderingIntent: CGColorRenderingIntent.defaultIntent) | |
var sourceBuffer = vImage_Buffer() | |
defer { | |
if #available(iOS 13.0, *) { | |
sourceBuffer.free() | |
} else { | |
sourceBuffer.data.deallocate() | |
} | |
} | |
var error = vImageBuffer_InitWithCGImage(&sourceBuffer, &format, nil, cgImage, numericCast(kvImageNoFlags)) | |
guard error == kvImageNoError else { return nil } | |
// create a destination buffer | |
let scale = self.scale | |
let destWidth = Int(size.width) | |
let destHeight = Int(size.height) | |
let bytesPerPixel = cgImage.bitsPerPixel / 8 | |
let destBytesPerRow = destWidth * bytesPerPixel | |
let destData = UnsafeMutablePointer<UInt8>.allocate(capacity: destHeight * destBytesPerRow) | |
defer { | |
destData.deallocate() | |
} | |
var destBuffer = vImage_Buffer(data: destData, height: vImagePixelCount(destHeight), width: vImagePixelCount(destWidth), rowBytes: destBytesPerRow) | |
// scale the image | |
error = vImageScale_ARGB8888(&sourceBuffer, &destBuffer, nil, numericCast(kvImageHighQualityResampling)) | |
guard error == kvImageNoError else { return nil } | |
// create a CGImage from vImage_Buffer | |
guard let destCGImage = vImageCreateCGImageFromBuffer(&destBuffer, &format, nil, nil, numericCast(kvImageNoFlags), &error)?.takeRetainedValue() else { return nil} | |
guard error == kvImageNoError else { return nil } | |
// create a UIImage | |
let resizedImage = UIImage(cgImage: destCGImage, scale: scale, orientation: self.imageOrientation) | |
return resizedImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great job! cool, respect!