如何创建CGImageRefs的可变数组? [英] How do I create a mutable array of 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?
推荐答案
通过获取CGImageRef出一个UIImage的中的 image.CGImage 的可能是昂贵的。从文档:
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 ::矢量的用于存储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屋!