How do I create a mutable array of CGImageRefs?(如何创建一个可变的 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 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何创建一个可变的 CGImageRefs 数组?


- android 4中的android RadioButton问题 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01