CGImageCreate is giving error in swift
Asked Answered
V

1

7

I'm trying to draw pixels of image into graphics context to iterate and print colorspace information values of an image. I've managed to buffer all the raw pixel values. But my attempts draw bitmap image using CGImageCreate have hit snag with the following error, also screenshot attached. This is my code written.

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 gives error at "kCGRenderingIntentDefault" saying "unresolved identifier". I have imported coreGraphics as well, it does not accept any value at this place. What can be suggested ?

Please help.

Volvulus answered 20/2, 2016 at 6:52 Comment(3)
I have managed to rectify CGContextDrawImage by type casting with CGFloat(variables). But now KCGRenderingIntent values for CGImageCreate is giving hard time.Volvulus
Shouldn't it be .RenderingIntentDefault in Swift?Coates
Using CGColorRenderingIntent.RenderingIntentDefault helped. Great. Thank you. Glenn Howes, you can post it as an answer so I could accept it ?Volvulus
C
7

Because of what the Swift SDK does to enumerations like this, you'll have to replace kCGRenderingIntentDefault with .defaultIntent as in:

let newImageRef = CGImageCreate(pixelWidth, pixelHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerPixel(imageRef), bytesPerRow, colorSpace, mapInfo, provider, nil, false, .defaultIntent)
Coates answered 21/2, 2016 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.