ios - CGContextDrawImage错误,如何分配缓冲区?

标签 ios swift buffer cgbitmapcontextcreate cgcontextdrawimage

我正在尝试将图像数据存储在我的应用程序的缓冲区中,以便我能够使用它,但是我在 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.

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

最佳答案

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)

关于ios - CGContextDrawImage错误,如何分配缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628003/

相关文章:

ios - iOS中的程序化无间隙音频序列

node.js - 将流写入缓冲区对象

ios - 我将在哪里获得用于实现 Azure B2c 身份验证的 kGraphURI 和 kScopes

ios - 更改 UITextField 宽度?

ios - Swift - 来自 Parse 的表重复记录

ios - 如何以编程方式让 UIWebView 暂停/播放视频内容

c - 连续扫描流缓冲区中字符串的最佳方法

javascript - 如何使用 NodeJS 将 PDF 缓冲区输出到浏览器?

ios - ImageView 向下滚动时缩放

swift - 枚举关联值令人困惑