如何创建一个可变的 CGImageRefs 数组? [英] How do I create a mutable array of CGImageRefs?

查看:24
本文介绍了如何创建一个可变的 CGImageRefs 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留一个可变的 CGImageRefs 集合.我是否需要将它们包装在 NSValue 中,如果需要,如何正确包装和打开它们?我可以摆脱使用 C 数组吗?如果是这样,我该如何构造它以及以后如何向其中添加元素?使用 UIImages 而不是 CGImageRefs 作为集合元素的成本是否明显更高?

I want to keep a mutable collection of CGImageRefs. Do I need to wrap them in NSValue, and if so how do I wrap and unwrap them properly? Can I get away with using a C array? If so how do I construct it and how do I add elements to it later? Is it significantly more costly to use UIImages instead of CGImageRefs as the elements of the collection?

推荐答案

通过 image.CGImage 从 UIImage 中获取 CGImageRef 可能代价高昂.来自文档:

Getting the CGImageRef out of an UIImage via image.CGImage can be costly. From the documentation:

如果图像数据因内存限制而被清除,则调用此方法会强制将该数据加载回内存.重新加载图像数据可能会导致性能损失.

If the image data has been purged because of memory constraints, invoking this method forces that data to be loaded back into memory. Reloading the image data may incur a performance penalty.

如果您对混合 C++ 和 Objective-C 感到满意,您可以使用 std::vector 来存储 CGImageRef.将源文件从 .m 重命名为 .mm 并尝试以下操作:

If you feel comfortable with mixing C++ and Objective-C, you can use a std::vector for storing the CGImageRef. Rename your source file from .m to .mm and try this:

#include <vector>
...
CGImageRef i;
...
std::vector<CGImageRef> images;
images.push_back(i);

如果您想将向量保留为 Objective-C 类的成员,您应该在堆上分配它,而不是在堆栈上:

If you want to keep the vector as a member of a Objective-C class, you should allocate it on the heap, not the stack:

头文件:

#include <vector>
using std;

@interface YourInterface : ...
{
   vector<CGImageRef> *images;
}

在实现文件中:

images = new std::vector<CGImageRef>();
images->push_back(i);
...
//When you're done
delete images;
images = NULL;

这篇关于如何创建一个可变的 CGImageRefs 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆