2015-09-17 21 views
13

के बाद CGBitmapInfo अल्फा मुखौटा मैं सर्वर से डाउनलोड की जा रही छवियों को कैश करने के लिए इस जिथब रेपो https://github.com/Haneke/HanekeSwift से लाइब्रेरी का उपयोग कर रहा हूं। तेजी से 2.0 अपडेट करने के बाद सब कुछ गड़बड़ हो गया है।स्विफ्ट 2.0

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

त्रुटि:

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

विशेष रूप से इस कोड है कि त्रुटियों फेंक रहे हैं की पंक्तियां हैं:

मैं सब कुछ है, लेकिन इस समारोह को ठीक कर लिया है एकल ऑपरेटर '~' 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

उत्तर

22

त्रुटि: की उम्मीद तर्क करने के लिए प्रकार का मान 'CGBitmapInfo' कनवर्ट नहीं कर सकता टाइप करें UInt32

स्विफ्ट 2.0 वास्तव में एक CGBitMapInfo ऑब्जेक्ट के बजाय UInt32 की अपेक्षा करता है, इसलिए आपको 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 तेज 2 CGBitmapInfo में एक OptionSetType है। एक को प्रारंभ करने के लिए आप एक CGBitmapInfo, 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 } 
} 

कैसे एक बाइट क्रम और अल्फा मुखौटा सेटिंग के साथ यह प्रारंभ करने का एक उदाहरण के अपने एक 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] 

का पालन कर सकते हैं:

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

related question, answer and comment

संबंधित मुद्दे