Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGImageCreate is giving error in swift

Tags:

ios

swift

pixels

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.

like image 782
Rajesh Mappu Avatar asked Feb 20 '16 06:02

Rajesh Mappu


1 Answers

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)
like image 84
Glenn Howes Avatar answered Sep 17 '22 17:09

Glenn Howes