How to create a mutable array of CGImageRefs?

I want to save a mutable collection of CGImageRefs. Do I need to wrap them in NSValue, and if so, how do I wrap and deploy them? Can I get away using an array of C? If so, how can I create it and how can I add elements to it later? Is it much more expensive to use UIImages instead of CGImageRefs as elements of a collection?

+3
source share
3 answers

Getting a CGImageRef from UIImage through image.CGImage can be expensive. From the documentation:

If image data has been cleared due to memory limitations, calling this method forces the data to be loaded back into memory. Reloading image data may result in poor performance.

If you are comfortable mixing C ++ and Objective-C, you can use std :: vector to store CGImageRef. Rename the source file from .m to .mm and try the following:

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

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

Header file:

#include <vector>
using std;

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

and in the implementation file:

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

You can directly add CGImage to NSMutableArray. You will need to specify (id) to avoid compiler warnings.

CFType NSObject. , NSObject CFType. , -retain -release .

+14

2011: just in case someone else is looking, you can wrap CGImageRef in NSValues ​​using

+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type

from here:

CGImageRef cgImage = [self cgImageMethod];
NSValue *cgImageValue = [NSValue valueWithBytes:&cgImage objCType:@encode(CGImageRef)];
[array addObject:cgImageValue];

to retrieve:

CGImageRef retrievedCGImageRef;
[[array objectAtIndex:0] getValue:&retrievedCGImageRef ];

hope this helps someone

+9
source

Source: https://habr.com/ru/post/1717117/


All Articles