开发者

Using NSData efficiently

开发者 https://www.devze.com 2023-04-08 00:14 出处:网络
I have a bit off code that manipulates Audio files by first putting all the audio file data in NSData variables. But it crashes sometimes because it uses to much RAM

I have a bit off code that manipulates Audio files by first putting all the audio file data in NSData variables. But it crashes sometimes because it uses to much RAM

 NSData *data1 = [NSData dataWithContentsOfFile: someFile];

I have checked using Instruments that everything was being released and how the RAM was being used and I figured out that it just crashes sometimes w开发者_JS百科hen the audio file are to big. Is there a way to store data in smaller bits or in the flash or any other way which would allow me to work with big files without exceeding the maximum RAM on the iPhone.

One thing Im using the NSData for example is concatenating 2 files like this:

 [data1 appendData: data2];

Thanks


Try this (from this SO question):

NSData* myBlob;
NSUInteger length = [myBlob length];
NSUInteger chunkSize = 100 * 1024;
NSUInteger offset = 0;
do {
    NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
    NSData* chunk = [NSData dataWithBytesNoCopy:(void*)[myBlob bytes] + offset
                                         length:thisChunkSize
                                   freeWhenDone:NO];
    offset += thisChunkSize;
    // do something with chunk
} while (offset < length);

Then, you could store each smaller chunk somewhere and do what you want with it later after concatenating.

This is all untested on my end, but it seems reasonable.


Is there a way to store data in smaller bits or in the flash or any other way which would allow me to work with big files without exceeding the maximum RAM on the iPhone.

of course. the problem is more fundamental than NSData - you don't usually load a set of audio files (in their entirety) into memory, especially when the device has very little memory. it's atypical to load the entire file even on osx, where you can have plenty of memory (unless the file is known to be very small). this is why the audio file apis allow you to read and write in blocks (ref: ExtAudioFileRead or AudioFileReadPackets).

0

精彩评论

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

关注公众号