CGBitmapInfo alpha mask after swift 2.0

2019-06-15 08:15发布

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)

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

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

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)

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

2条回答
仙女界的扛把子
2楼-- · 2019-06-15 08:23

error:Cannot convert value of type 'CGBitmapInfo' to expected argument of type 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

查看更多
仙女界的扛把子
3楼-- · 2019-06-15 08:44

OptionSetType In swift 2 CGBitmapInfo is an OptionSetType. To initialize one you can follow the convention

///       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]

in the case of a CGBitmapInfo, its an OptionSetType of OptionSetTypes

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

an example of how to initialize it with a byte order and alpha mask setting:

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

related question, answer and comment

查看更多
登录 后发表回答