2014-06-08 14 views
25

Próbuję utworzyć CGContext w szybkim tempie. Kompiluje, ale zgłasza błąd w czasie wykonywania.CGBitmapContextUtwórz błąd ze swiftem

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

.... 

a błąd jest:

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 

Odpowiedz

56

Na wypadek, gdyby ktoś wpadł na ten sam problem. Poniższy fragment w końcu działa.

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) 

Generuje 32 bitową kontekst RGBA w Swift

+6

Z aktualną wersją Swift (Używam Xcode 6.1): niech bitmapInfo = CGBitmapInfo (CGImageAlphaInfo.PremultipliedLast.rawValue) –

+0

@RyanH. - Dzięki - mam zamiar edytować to w odpowiedzi na: – Robert

+4

I przy obecnej wersji Xcode jest teraz: let context = CGBitmapContextCreate (zero, Int (rect.size.width), Int (rect.size.height), 8, 0, colorSpace, bitmapInfo) –

0

CGBitmapInfo.AlphaInfoMask nie jest poprawnym informacji bitmapy.

Spróbuj ustawić CGBitmapInfo.AlphaLast lub CGBitmapInfo.AlphaFirst.

+0

To się nie kompiluje i nie mogę znaleźć CGBitmapInfo.AlphaLast lub CGBitmapInfo.AlphaFirst w https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGBitmapContext/index.html#//apple_ref/c/func/CGBitmapContextCreate – loopmasta

3

miałem pewne problemy w Swift 1.2 używając UInt, teraz używam Int i to działa. Ten przykład pokazuje, jak konwertować obraz na obraz w skali szarości.

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

w Swift 2.1 można uzyskać dostęp do pól poprawnie, a nawet LUB je razem:

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

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

całe mnóstwo 'RawValue' dzieje :)

Nie trzeba nawet wydzielić bitmapInfo, a może zrobić jedno-liner:

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

w 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

Aktualizacja 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

Sugerowany sposób kompatybilny zarówno Xcode 8,3 i Xcode 9 który wspiera Swift 3 i 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 
    } 
Powiązane problemy