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!

Community
  • 1
  • 1
Ted Lasso
  • 131
  • 2
  • 10
  • 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. –  Apr 05 '16 at 04:28
  • 1
    Great point. I'll get to that right away. – Ted Lasso Apr 05 '16 at 14:10

2 Answers2

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


Hvordan har du det
  • 3,605
  • 2
  • 25
  • 48
  • 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? – Ted Lasso Apr 05 '16 at 14:10
-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))
Christian Abella
  • 5,747
  • 2
  • 30
  • 42