0

我正在尝试将图像数据存储在我的应用程序的缓冲区中,因此我将能够使用它,但是我在 CGContextDrawImage 行上收到 EXC_BAD_ACCESS 错误。这是我正在使用的代码:

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
}

当尝试将缓冲区和字节设置为 null 和 0 时,如用于自动内存分配的苹果文档中的应用程序不会崩溃,但会出现以下错误:

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.

好像我错过了一些东西,但无法真正弄清楚,需要任何建议,谢谢!

4

1 回答 1

0

在Peacer212的帮助下完成了这项工作,一切似乎都按需要工作,没有任何错误,这是一个工作代码:

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)
于 2015-09-18T07:03:48.747 回答