2016-02-20 18 views
6

Próbuję narysować piksele obrazu w kontekście graficznym, aby iterować i drukować wartości informacji o przestrzeni kolorów obrazu. Udało mi się zbuforować wszystkie surowe wartości pikseli. Ale moje próby narysować obraz bitmapowy przy użyciu CGImageCreate hit hak z następującego błędu, również screenshot attached. To jest mój kod napisany.CGImageCreate daje błąd w szybkim

func createRGBAPixel(inImage: CGImageRef) -> CGContextRef { 
//Image width, height 

let pixelWidth = CGImageGetWidth(inImage) 
let pixelHeight = CGImageGetHeight(inImage) 

//Declaring number of bytes 

let bytesPerRow = Int(pixelWidth) * 4 
let byteCount = bytesPerRow * Int(pixelHeight) 

//RGB color space 

let colorSpace = CGColorSpaceCreateDeviceRGB() 

//Allocating image data 

let mapData = malloc(byteCount) 
let mapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue) 

//Create bitmap context 

let context = CGBitmapContextCreate(mapData, pixelWidth, pixelHeight, Int(8), Int(bytesPerRow), colorSpace, mapInfo.rawValue) 

let imageRef = CGBitmapContextCreateImage(context) 


CGContextDrawImage(context, CGRectMake(0, 0, pixelWidth, pixelHeight), imageRef) 


let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData 
let pixels = UnsafePointer<UInt8>(data.bytes) 
var newPixelsArray = [UInt8](count: byteCount, repeatedValue: 0) 
let provider = CGDataProviderCreateWithData(nil , &newPixelsArray, data.length, nil) 
let newImageRef = CGImageCreate(pixelWidth, pixelHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerPixel(imageRef), bytesPerRow, colorSpace, mapInfo, provider, nil, false, kCGRenderingIntentDefault) 


return context! 

CGImageCreate daje błąd w "kCGRenderingIntentDefault" powiedzenie "nierozwiązane identyfikator". Zaimportowałem także coreGraphics, nie akceptuję żadnej wartości w tym miejscu. Co można zasugerować?

Proszę o pomoc.

+0

udało mi się naprawić CGContextDrawImage według rodzaju odlewu z CGFloat (zmiennych). Ale teraz wartości KCGRenderingIntent dla CGImageCreate dają trudny czas. –

+0

Nie powinno być .RenderingIntentDefault w Swift? –

+0

Pomógł przy użyciu narzędzia CGColorRenderingIntent.RenderingIntentDefault. Wspaniały. Dziękuję Ci. Glenn Howes, możesz napisać to jako odpowiedź, abym mógł to zaakceptować? –

Odpowiedz

6

Ze względu na to, co robi Swift SDK do wyliczenia jak to, trzeba będzie zastąpić kCGRenderingIntentDefault z .defaultIntent jak w:

let newImageRef = CGImageCreate(pixelWidth, pixelHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerPixel(imageRef), bytesPerRow, colorSpace, mapInfo, provider, nil, false, .defaultIntent) 
+0

Zwróć uwagę, że w swift 3 jest teraz '.defaultIntent' – Guig

+0

Dzięki, zaktualizowałem, aby to pokazać. –