ios - swift 2.0 之后的 CGBitmapInfo alpha 掩码

标签 ios swift swift2 alpha-transparency

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

除了这个功能,我已经能够修复所有问题:

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
    }
}

具体来说,这是抛出错误的代码行:

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

错误:一元运算符 '~' 不能应用于 CGBitmapInfo 类型的操作数

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

错误:二元运算符“|=”不能应用于“CGBitmapInfo”类型的操作数

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

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

最佳答案

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

Swift 2.0 实际上需要一个 UInt32 而不是 CGBitMapInfo 对象,所以你应该在 CGBitMapInfo 中取出一个 UInt32 变量。

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

关于ios - swift 2.0 之后的 CGBitmapInfo alpha 掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32639743/

相关文章:

ios - Swift 和 EXT_BAD_ACCESS 中的详细文本标签

ios - 使用未解析的标识符 'segue'

ios - 我怎么知道 UNUserNotificationCenter 的 removeAllPendingNotificationRequests() 何时完成?

iOS - 为什么我不能以相同的方式使用 pushViewController 两次?

ios - 如何在 iOS 中为自定义键盘制作删除按钮?

ios - 通过 JTAppleCalendar 将月份和年份显示为标题

swift - 实现公共(public)接口(interface)的类型的通用函数

xcode - Swift:按多个标准(权重)对数组进行排序

ios - 在 Swift 中的浏览器中打开 Web url 链接

ios - UICollectionView prepareForSegue indexPathForCell 为零 iOS 9