Swift:UIGraphics Scale Down CGImage

From Hawk Wiki
Revision as of 04:14, 23 March 2015 by Hall (Talk | contribs) (Created page with "<pre class="brush:swift"> func scaleDownCGImage(image: CGImage, scale: Float) -> CGImage!{ var scaleDiv = UInt(1.0 / scale) let width = CGImageGetWidth(image) / scaleD...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
func scaleDownCGImage(image: CGImage, scale: Float) -> CGImage!{
    var scaleDiv = UInt(1.0 / scale)
    let width = CGImageGetWidth(image) / scaleDiv
    let height = CGImageGetHeight(image) / scaleDiv
    let bitsPerComponent = CGImageGetBitsPerComponent(image)
    let bytesPerRow = CGImageGetBytesPerRow(image)
    let colorSpace = CGImageGetColorSpace(image)
    let bitmapInfo = CGImageGetBitmapInfo(image)
    let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
    CGContextSetInterpolationQuality(context, kCGInterpolationMedium)
    let imgSize = CGSize(width: Int(width), height: Int(height))
    CGContextDrawImage(context, CGRect(origin: CGPointZero, size: imgSize), image)
    return CGBitmapContextCreateImage(context)
}