2015-09-17 11 views
13

Sto utilizzando una libreria da questo repository github https://github.com/Haneke/HanekeSwift per memorizzare nella cache le immagini scaricate da un server. Dopo l'aggiornamento a swift 2.0, tutto è incasinato.CGBitmapInfo alpha mask after swift 2.0

Sono stato in grado di risolvere tutto, ma questa funzione:

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

In particolare ciò sono le righe di codice che stanno gettando gli errori:

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

errore: operatore unario '~' non può essere applicato a operando di tipo CGBitmapInfo

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

: operatore binario '| =' non può essere applicato ad un operando di tipo di errore

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

'CGBitmapInfo': Impossibile convertire il valore di tipo 'CGBitmapInfo' all'argomento previsto di tipo UInt32

risposta

22

errore: Impossibile convertire il valore di tipo 'CGBitmapInfo' all'argomento previsto di digitare UInt32

Swift 2.0 in effetti si aspetta un UInt32 anziché un oggetto CGBitMapInfo, quindi è necessario estrarre una variabile UInt32 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

OptionSetType In rapida 2 CGBitmapInfo è un OptionSetType. Per inizializzare quella che si può seguire la convenzione

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

nel caso di un CGBitmapInfo, la sua un'OptionSetType di 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 } 
} 

un esempio di come inizializzare con un ambiente ordine di byte e maschera alfa:

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

related question, answer and comment