iPhone - 获取指向 CGDataProvider 背后的数据的指针?

发布于 2024-08-30 09:53:56 字数 513 浏览 10 评论 0原文

我正在尝试获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-09-06 09:53:56

看这个答案:
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.

感受沵的脚步 2024-09-06 09:53:56

除非你在使用Instruments测量时发现性能问题,否则我不认为复制两次是一个大问题。您始终可以直接存储 CFDataRef imgData 而不是 mybuff。

编辑
我说的是这种代码:
在你的类接口中添加一个数组:

NSMutableArray *images;

在你的 init: 方法中:创建数组:

images = [[NSMutableArray alloc] init];

并有一个 dealloc;

-(void)dealloc{
    [images release];
}

并在保存每个图像的代码中:

CGDataRef imageDataRef = CGDataProviderCopyData(CGImageGetDataProvider(myCGImageRef)));
[images addObject: (NSData*)imageDataRef];
CGRelease(imageDataRef);

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:

NSMutableArray *images;

In your init: method: create the array:

images = [[NSMutableArray alloc] init];

and have a dealloc;

-(void)dealloc{
    [images release];
}

and in your code that saves each image:

CGDataRef imageDataRef = CGDataProviderCopyData(CGImageGetDataProvider(myCGImageRef)));
[images addObject: (NSData*)imageDataRef];
CGRelease(imageDataRef);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文