本文介绍了迅速2.0之后的CGBitmapInfo alpha蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此github存储库中的库 https://github.com/Haneke/HanekeSwift 缓存正在从服务器下载的图像.更新到Swift 2.0之后,一切都搞砸了.

I am using a library from this github repo https://github.com/Haneke/HanekeSwift to cache the images that are being downloaded from a server. After updating to swift 2.0 everything is messed up.

除此功能外,我已经可以解决所有问题:

I've been able to fix everything but this function:

func hnk_decompressedImage() -> UIImage! {
    let originalImageRef = self.CGImage
    let originalBitmapInfo = CGImageGetBitmapInfo(originalImageRef)
    let alphaInfo = CGImageGetAlphaInfo(originalImageRef)

    // See: http://stackoverflow.com/questions/23723564/which-cgimagealphainfo-should-we-use
    var bitmapInfo = originalBitmapInfo
    switch (alphaInfo) {
    case .None:
        bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
        bitmapInfo |= CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)
    case .PremultipliedFirst, .PremultipliedLast, .NoneSkipFirst, .NoneSkipLast:
        break
    case .Only, .Last, .First: // Unsupported
        return self
    }

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let pixelSize = CGSizeMake(self.size.width * self.scale, self.size.height * self.scale)
    if let context = CGBitmapContextCreate(nil, Int(ceil(pixelSize.width)), Int(ceil(pixelSize.height)), CGImageGetBitsPerComponent(originalImageRef), 0, colorSpace, bitmapInfo) {

        let imageRect = CGRectMake(0, 0, pixelSize.width, pixelSize.height)
        UIGraphicsPushContext(context)

        // Flip coordinate system. See: http://stackoverflow.com/questions/506622/cgcontextdrawimage-draws-image-upside-down-when-passed-uiimage-cgimage
        CGContextTranslateCTM(context, 0, pixelSize.height)
        CGContextScaleCTM(context, 1.0, -1.0)

        // UIImage and drawInRect takes into account image orientation, unlike CGContextDrawImage.
        self.drawInRect(imageRect)
        UIGraphicsPopContext()
        let decompressedImageRef = CGBitmapContextCreateImage(context)

        let scale = UIScreen.mainScreen().scale
        let image = UIImage(CGImage: decompressedImageRef, scale:scale, orientation:UIImageOrientation.Up)

        return image

    } else {
        return self
    }
}

具体来说,这是引发错误的代码行:

Specifically this are the lines of code that are throwing the errors:

bitmapInfo &= ~CGBitmapInfo.AlphaInfoMask
        bitmapInfo |= CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)

错误:一元运算符'〜'无法应用于CGBitmapInfo类型的操作数

error: Unary operator '~'cannot be applied to operand of type CGBitmapInfo

bitmapInfo |= CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)

错误:二进制运算符'| ='无法应用于类型为'CGBitmapInfo'的操作数

error: Binary operator '|=' cannot be applied to an operand of type 'CGBitmapInfo'

if let context = CGBitmapContextCreate(nil, Int(ceil(pixelSize.width)), Int(ceil(pixelSize.height)), CGImageGetBitsPerComponent(originalImageRef), 0, colorSpace, bitmapInfo)

错误:无法将'CGBitmapInfo'类型的值转换为UInt32类型的预期参数

error:Cannot convert value of type 'CGBitmapInfo' to expected argument of type UInt32

推荐答案

错误:无法将'CGBitmapInfo'类型的值转换为UInt32类型的预期参数

error:Cannot convert value of type 'CGBitmapInfo' to expected argument of type UInt32

Swift 2.0实际上期望使用UInt32而不是CGBitMapInfo对象,因此您应该在CGBitMapInfo中删除UInt32变量.

Swift 2.0 actually expects a UInt32 instead of a CGBitMapInfo object, so you should take out a UInt32 Variable in CGBitMapInfo.

CGBitmapContextCreate(
    nil,
    Int(ceil(pixelSize.width)),
    Int(ceil(pixelSize.height)),
    CGImageGetBitsPerComponent(originalImageRef),
    0,
    colorSpace,
    bitmapInfo.rawValue)

https://developer.apple. com/library/ios/documentation/GraphicsImaging/Reference/CGBitmapContext/#//apple_ref/c/func/CGBitmapContextCreate

这篇关于迅速2.0之后的CGBitmapInfo alpha蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 17:04