Swift 2.0之后的CGBitmapInfo alpha掩码

14

我正在使用来自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: https://dev59.com/-WAg5IYBdhLWcg3wU5m8
    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: https://dev59.com/RnRB5IYBdhLWcg3wz6QJ
        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的预期参数

2个回答

22

错误:无法将'type '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


3

OptionSetType 在Swift 2中,CGBitmapInfo是一个OptionSetType。要初始化一个OptionSetType,您可以按照以下约定进行操作:

///       static let Box = PackagingOptions(rawValue: 1)
///       static let Carton = PackagingOptions(rawValue: 2)
///       static let Bag = PackagingOptions(rawValue: 4)
///       static let Satchel = PackagingOptions(rawValue: 8)
///       static let BoxOrBag: PackagingOptions = [Box, Bag]
///       static let BoxOrCartonOrBag: PackagingOptions = [Box, Carton, Bag]

在CGBitmapInfo的情况下,它是一个OptionSetTypes的OptionSetType。
public enum CGImageAlphaInfo : UInt32 {

    case None /* For example, RGB. */
    case PremultipliedLast /* For example, premultiplied RGBA */
    case PremultipliedFirst /* For example, premultiplied ARGB */
    case Last /* For example, non-premultiplied RGBA */
    case First /* For example, non-premultiplied ARGB */
    case NoneSkipLast /* For example, RBGX. */
    case NoneSkipFirst /* For example, XRGB. */
    case Only /* No color data, alpha data only */
}

@available(iOS 2.0, *)
public struct CGBitmapInfo : OptionSetType {
    public init(rawValue: UInt32)

    public static var AlphaInfoMask: CGBitmapInfo { get }
    public static var FloatComponents: CGBitmapInfo { get }

    public static var ByteOrderMask: CGBitmapInfo { get }
    public static var ByteOrderDefault: CGBitmapInfo { get }
    public static var ByteOrder16Little: CGBitmapInfo { get }
    public static var ByteOrder32Little: CGBitmapInfo { get }
    public static var ByteOrder16Big: CGBitmapInfo { get }
    public static var ByteOrder32Big: CGBitmapInfo { get }
}

以下是使用字节顺序和 alpha 掩码设置进行初始化的示例:

    let bitmapInfo:CGBitmapInfo = [.ByteOrder32Little, CGBitmapInfo(rawValue: ~CGBitmapInfo.AlphaInfoMask.rawValue | CGImageAlphaInfo.PremultipliedFirst.rawValue)]

related question, answer and comment


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接