【问题标题】:CGBitmapContextGetData - can't copy data to the returned block of memoryCGBitmapContextGetData - 无法将数据复制到返回的内存块
【发布时间】:2012-10-01 16:54:31
【问题描述】:

我正在使用CGBitmapContextCreateCGContextDrawImage 在屏幕上绘制RGBA 数据。当我尝试使用CGBitmapContextCreate(pixelBuffer,...) 创建bitmapcontext 时,我已经有malloc'ed pixelBuffer 并将我的数据放在那里,这工作得很好。

但是,我希望 Core Graphics 管理自己的内存,所以我想将 NULL 传递给 CGBitmapContextCreate,然后通过调用 CGBitmapContextGetData 获取指向使用的内存块的指针,并复制我的 @987654332使用memcpy @buffer 到上述块。但是,我的 memcpy 失败了。请在下面查看我的代码。 知道我做错了什么吗?

gtx = CGBitmapContextCreate(NULL, screenWidth, screenHeight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipLast);
void *data = CGBitmapContextGetData(gtx);
memcpy(data, pixelBuffer, area*componentsPerPixel);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGImageRef image = CGBitmapContextCreateImage(gtx);
CGContextTranslateCTM(currentContext, 0, screenHeight);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawImage(currentContext, currentSubrect, image);

【问题讨论】:

  • 如果传递你自己的指针有效,为什么不坚持呢?
  • 我相信传递 null 更为理想,并且我可能会获得绘图性能改进(这对我的应用程序很重要)。更重要的是,我偶尔会看到应用程序因 vm_copy 失败而崩溃,我怀疑这可能与我传递自己的指针有关(只是直觉)。
  • 顺便说一句——不相关,但如果您调整应用程序的性能并注意到绘图代码中有大量 RGBAToARGB 调用,请尝试更改位图格式以匹配 CG 本身想要的格式。有道理?这可能因 CPU(模拟器与设备)而异
  • Sep 19 14:47:35 unknown XYZ[45240] : CGDataProviderCreateWithCopyOfData: vm_copy failed: status 1.Sep 19 14:47:35 unknown UIKitApplication:com.xyz.XYZ[0x91dc] [45240] :9 月 19 日 14:47:35 xyz's-iPad XYZ[45240] :CGDataProviderCreateWithCopyOfData:vm_copy 失败:状态 1。 > 9 月 19 日 14:47:36 未知 ReportCrash[45631] :为进程 XYZ[45240] 制定崩溃报告 9 月 19 日 14:47:36 未知 com.apple.launchd[1] : (UIKitApplication:com.xyz.XYZ[0x91dc]) 作业似乎已崩溃:分段错误: 11
  • 尝试将分配的大小设置为round_page( original_size )

标签: objective-c core-graphics memcpy cgbitmapcontextcreate


【解决方案1】:

根据我所有的研究,使用 drawrect 频繁/重复绘制是一个坏主意,所以我决定转向基于 UIImageView 的绘图,正如对其他 SO question 的回复中所建议的那样。

这是关于为什么 UIImageView 比 drawrect 更有效的另一个 SO 答案。

基于上述情况,我现在使用 UIImageView 而不是 drawrect,并且看到了更好的绘图性能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2017-12-23
    • 2015-03-11
    • 2023-03-13
    • 1970-01-01
    • 2013-08-02
    • 2014-09-04
    相关资源
    最近更新 更多