0

I am trying to store image data in buffer in my app so i will be able to use it however I get EXC_BAD_ACCESS error on CGContextDrawImage line. Here is the code i am using:

resizedImage.Array() // resizedImage - resized image of 280x140 pixels size
    func Array() {
        let dataBuffer = UnsafeMutablePointer<CUnsignedChar>.alloc(156800) //alloc(156800)
        memset(dataBuffer, 0, 156800)
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue)
        let context = CGBitmapContextCreate(dataBuffer, 280, 140, 8, 156800, colorSpace, bitmapInfo.rawValue)
        let imageRef = CGImageCreateWithImageInRect(CGImage, CGRectMake(0, 0, 280, 140))
        let rectMake = CGRectMake(0, 0, 280, 140)
        CGContextDrawImage(context, rectMake, imageRef)

return
}

When trying to set buffer and bytes as null and 0 as in apple documentation for automatic memory allocation app doesn't crash, but it gives this errors:

Sep 17 17:18:33  VideoTester[4846] <Error>: CGBitmapContextCreate: invalid data bytes/row: should be at least 1120 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedLast.
Sep 17 17:18:33  VideoTester[4846] <Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Seems like I am missing something but can't really figure it out, any advice is needed, thank you!

6
  • 1
    The first thing I noticed was the bytesPerRow parameter in your CGBitmalContextCreate function, which I guess should be bytesPerPixel * width, with bytesPerPixel being 4 bytes (rgba each 1 byte).
    – peacer212
    Commented Sep 17, 2015 at 10:42
  • also check stackoverflow.com/questions/24109149/…
    – peacer212
    Commented Sep 17, 2015 at 10:45
  • thank you for you answer, setting "bytesPerPixel * width: gives that error: malloc: *** error for object 0x136060e08: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Commented Sep 17, 2015 at 10:59
  • And setting buffer to 0 as via link is not crashing but gives that invalid data bytes/row errors in logs Commented Sep 17, 2015 at 11:02
  • I believe you don't necessarily need to use an unretained pointer, try the following: var dataBuffer = [UInt8](count: 280*140, repeatedValue: 0) CGBitmapContextCreate(&dataBuffer,...)
    – peacer212
    Commented Sep 17, 2015 at 11:03

1 Answer 1

0

Got this working with a help of peacer212, everything seems to work as needed without any errors, here is a working code:

var dataBuffer = [UInt8](count: 280*140 * 4, repeatedValue: 0)

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue)

        let context = CGBitmapContextCreate(&dataBuffer, 280, 140, 8, 1120, colorSpace, bitmapInfo.rawValue)
        let imageRef = CGImageCreateWithImageInRect(CGImage, CGRectMake(0, 0, 280, 140))
        let rectMake = CGRectMake(0, 0, 280, 140)

        CGContextDrawImage(context, rectMake, imageRef)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.