2014-06-08 15 views
25

Sto cercando di creare un CGContext in swift. Compila ma genera un errore in fase di esecuzione.Errore CGBitmapContextCreate con swift

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB() 
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask) 
CGColorSpaceRelease(colorSpace); 

.... 

e l'errore viene:

Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 96 bytes/row. 
fatal error: Can't unwrap Optional.None 

risposta

56

Nel caso in cui qualcuno si imbattesse nello stesso problema. Lo snippet qui sotto funziona finalmente.

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB() 
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) 
let context = CGBitmapContextCreate(nil, UInt(rect.size.width), UInt(rect.size.height), 8, 0, colorSpace, bitmapInfo) 

genera un contesto RGBA a 32 bit in rapida

+6

Con la versione corrente di Swift (Sto usando Xcode 6.1): let bitmapInfo = CGBitmapInfo (CGImageAlphaInfo.PremultipliedLast.rawValue) –

+0

@RyanH. - Grazie - Ho intenzione di modificare questo nella risposta – Robert

+4

E con la versione attuale di Xcode è ora: let context = CGBitmapContextCreate (nil, Int (rect.size.width), Int (rect.size.height), 8, 0, colorSpace, bitmapInfo) –

0

CGBitmapInfo.AlphaInfoMask non è un informazioni bitmap valido.

Provare a impostare CGBitmapInfo.AlphaLast o CGBitmapInfo.AlphaFirst.

+0

Questo non viene compilato e non riesco a trovare CGBitmapInfo.AlphaLast oppure CGBitmapInfo.AlphaFirst in https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGBitmapContext/index.html#//apple_ref/c/func/CGBitmapContextCreate – loopmasta

3

Ho avuto alcuni problemi a Swift 1.2 utilizzando UInt, ora sto usando Int e che sta funzionando. Questo esempio mostra come convertire un'immagine in un'immagine in scala di grigi.

let imageRect = self.myImage.frame 

    let colorSpace = CGColorSpaceCreateDeviceGray() 
    let width = imageRect.width 
    let height = imageRect.height 

    let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.None.rawValue) 
    let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo) 
10

In Swift 2.1 si può accedere ai campi correttamente, e pari o insieme:

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue) 

let context = CGBitmapContextCreate(baseAddress, width, height, 8, 
       bytesPerRow, colorSpace, bitmapInfo.rawValue); 

sacco intero di 'rawValue' succedendo :)

Non hai nemmeno bisogno per separare il BITMAPINFO, e può fare una battuta:

let context = CGBitmapContextCreate(baseAddress, width, height, 8, 
       bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue 
1

In Swift 2.2:

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue 
let colorSpace = CGColorSpaceCreateDeviceRGB() 
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo) 
14

aggiornato per Swift 3:

let colorSpace = CGColorSpaceCreateDeviceRGB() 
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) 
    guard let context = CGContext.init(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: Int(bitsPerComponent), bytesPerRow: Int(bytesPerRow), space: colorSpace, bitmapInfo: UInt32(bitmapInfo.rawValue)) else { 
     // cannot create context - handle error 
    } 
2

modo suggerito compatibile sia con Xcode 8.3 e Xcode 9 che supporta Swift 3 e Swift 4

let colorSpace = CGColorSpaceCreateDeviceRGB() 
guard let bitmapContext = CGContext(data: nil, 
            width: Int(size.width), 
            height: Int(size.height), 
            bitsPerComponent: Int(bitsPerComponent), 
            bytesPerRow: Int(bytesPerRow), 
            space: colorSpace, 
            bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { 
            return nil 
    } 
Problemi correlati