【发布时间】:2012-10-01 16:54:31
【问题描述】:
我正在使用CGBitmapContextCreate 和CGContextDrawImage 在屏幕上绘制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