4

all!

I've been doing a lot of research into this and I've integrated several different solutions into my project, but none of them seem to work. My current solution has been borrowed from this thread.

When I run my code, however, two things happen:

  1. The pixel array remains initialized but unpopulated (Full of 0s)
  2. I get two errors:

    CGBitmapContextCreate: unsupported parameter combination: set CGBITMAP_CONTEXT_LOG_ERRORS environmental variable to see the details

and

CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set 

Any ideas? Here is my current built function that I'm calling for my Image class:

init?(fromImage image: UIImage!) {
    let imageRef = image!.CGImage
    self.width = CGImageGetWidth(imageRef)
    self.height = CGImageGetHeight(imageRef)
    let colorspace = CGColorSpaceCreateDeviceRGB()
    let bytesPerRow = (4 * width);
    let bitsPerComponent :UInt = 8
    let pixels = UnsafeMutablePointer<UInt8>(malloc(width*height*4))

    var context = CGBitmapContextCreate(pixels, width, height, Int(bitsPerComponent), bytesPerRow, colorspace, 0);

    CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)

Any pointers would help a lot, as I'm new to understanding how all of this CGBitmap stuff works.

Thanks a ton!

2
  • First step would be to remove the implicitly unwrapped and force unwrapped Optional. They are just asking for runtime crashes and errors. It's much better to check an Optional and properly handle each case. Commented Apr 5, 2016 at 4:28
  • 1
    Great point. I'll get to that right away. Commented Apr 5, 2016 at 14:10

2 Answers 2

4

You should not pass an 0 as the bitmapInfo param for CGBitmapContextCreate. For RGBA you shall pass CGImageAlphaInfo.PremultipliedLast.rawValue.


Supported combinations of bitsPerComponent, bytesPerRow, colorspace and bitmapInfo can be found here: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-BCIBHHBB

Note that 32 bits per pixel (bpp) is 4 bytes per pixel and you use it to calculate bytesPerRow


Sign up to request clarification or add additional context in comments.

1 Comment

This definitely helped a ton for getting the array populated, although I'm not sure it's being populated correctly. When I convert it back into an image straight away, the channels seems very messed up. Should I continue this here or start another question?
-1

You need to convert the image to NSData and then convert NSData to UInt8 array.

let data: NSData = UIImagePNGRepresentation(image)

// or let data: NSData = UIImageJPGRepresentation(image)

let count = data.length / sizeof(UInt8)

// create an array of Uint8
var array = [UInt8](count: count, repeatedValue: 0)

// copy bytes into array
data.getBytes(&array, length:count * sizeof(UInt8))

1 Comment

That would give you the PNG representation, not the raw pixel data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.