How do I create a mutable array of CGImageRefs?

后端 未结 3 1004
眼角桃花
眼角桃花 2020-12-21 01:21

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?

3条回答
  •  生来不讨喜
    2020-12-21 01:57

    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.

    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 
    ...
    CGImageRef i;
    ...
    std::vector images;
    images.push_back(i);
    

    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:

    Header file:

    #include 
    using std;
    
    @interface YourInterface : ...
    {
       vector *images;
    }
    

    and in the implementation file:

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

提交回复
热议问题