iPhone - 获取指向 CGDataProvider 背后的数据的指针?
我正在尝试获取 CGImage 并将其数据复制到缓冲区中以供以后处理。下面的代码是我到目前为止所拥有的,但有一点我不喜欢它 - 它复制图像数据两次。一次用于 CGDataProviderCopyData(),一次用于对 imgData 的 :getBytes:length
调用。我无法找到一种方法将图像数据直接复制到缓冲区中并删除 CGDataProviderCopyData() 步骤,但必须有一种方法......有任何指针吗? (...双关语)
NSData *imgData = (NSData *)(CGDataProviderCopyData(CGImageGetDataProvider(myCGImageRef))); CGImageRelease(myCGImageRef); // i've got a previously-defined pointer to an available buffer called "mybuff" [imgData getBytes:mybuff length:[imgData length]];
I'm trying to take a CGImage and copy its data into a buffer for later processing. The code below is what I have so far, but there's one thing I don't like about it - it's copying the image data twice. Once for CGDataProviderCopyData()
and once for the :getBytes:length
call on imgData. I haven't been able to find a way to copy the image data directly into my buffer and cut out the CGDataProviderCopyData()
step, but there has to be a way...any pointers? (...pun ftw)
NSData *imgData = (NSData *)(CGDataProviderCopyData(CGImageGetDataProvider(myCGImageRef))); CGImageRelease(myCGImageRef); // i've got a previously-defined pointer to an available buffer called "mybuff" [imgData getBytes:mybuff length:[imgData length]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看这个答案:
CGDataProviderCopyData() 实际上复制字节吗?或者只是指针?
看起来对 CGDataProvider 后面的缓冲区的直接访问仅限于私有 API。
See this answer:
Does CGDataProviderCopyData() actually copy the bytes? Or just the pointer?
Looks like direct access to the buffer behind a CGDataProvider is restricted to private APIs.
除非你在使用Instruments测量时发现性能问题,否则我不认为复制两次是一个大问题。您始终可以直接存储 CFDataRef imgData 而不是 mybuff。
编辑
我说的是这种代码:
在你的类接口中添加一个数组:
在你的 init: 方法中:创建数组:
并有一个 dealloc;
并在保存每个图像的代码中:
Unless you see a performance problem when you use Instruments to measure it, I don't think that copying it twice is a huge problem. You could always store the CFDataRef imgData directly instead of the mybuff.
Edit
I'm talking about this kind of code:
In your class interface add an array:
In your init: method: create the array:
and have a dealloc;
and in your code that saves each image: