开发者

What is a good way to store 100,000+ ints (representing image delta data)?

I've searched the forum and seen some possible pa开发者_如何转开发rtial solutions to this question, but I'd like help putting it all together.

I'm getting the frames from the camera and doing image processing on the difference between the current frame and previous frame. In addition to the RGB values from the camera, I'm also calculating Hue and Saturation for each pixel, each of which is also an int. So my 2 questions are:

  1. What is the best way to store all of these values from each call to didOutputSampleBuffer? From what I've been reading, it seems like with this many values, the overhead from NSNumber will be noticable so least memory would be spent using a classic c-style array of ints w/ length 144 x 192 x 5(R,G,B,H,S) = 138,240. Does that make sense?

  2. How do I put this array in the scope of my didOutputSampleBuffer method, because I'm initializing the array upon app launch, not in the didOutputSampleBuffer method. Someone on the forum mentioned perhaps I could wrap the array in NSMutableData and then i could just store it as a property?

Thank you for your advice, Don


Given that the size of an image won't change, you should be able to create a buffer to store these components as interleaved bytes or a few buffers for each color component plane. You could do this by manually using malloc() and free() to create this buffer and destroy it when done.

If you'd prefer to use reference counting, you could wrap these bytes in an NSData instance, which won't add much overhead to your processing. Either a pointer to your processed buffer bytes or an NSData instance could be used as properties.

Note that you'll probably want to use unsigned char types for each component, because you're only getting back individual bytes for each of the color components. Why waste memory with unnecessary precision?


  1. Yes, that is a good way to store the data. Alternatively, you could use a c array of structures (see my example).
  2. You could use a global variable or a property, containing either a NSMutableData object or the pointer to the array. Since you want access to the data as integers and not raw data, storing the pointer to the data would probably be easier than a NSData object.

Example:

// header file
struct PixelData {
    int r, g, b, h, s;
};
@interface TheClass : TheSuperclass {
    struct PixelData *dataPointer;
}
@property struct PixelData *dataPointer;
@end

// implementation file
@implementation TheClass
@synthesize dataPointer;
- (void)didOutputSampleBuffer { // Yes, I know this isn't the full name.
    // parse data
    // store data for pixel at index i:
    struct PixelData *dp = self.dataPointer;
    dp[i].r = r;
    dp[i].g = g;
    dp[i].b = b;
    dp[i].h = h;
    dp[i].s = s;
}
@end


When I was dealing with a similar problem I simply made the c style array an ivar on an object.

This way I could attach additional properties to it like metadata, etc.

@interface MyObject : NSObject {

    int *arrayOfInts;
}

@property (readwrite) int *arrayOfInts;

@end

You still have to explicitly manage the memory in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜