高效使用 NSData
我有一些代码,通过首先将所有音频文件数据放入 NSData 变量来操作音频文件。但它有时会崩溃,因为它使用了太多的 RAM
NSData *data1 = [NSData dataWithContentsOfFile: someFile];
我已经使用 Instruments 检查了所有内容都被释放以及 RAM 的使用情况,我发现当音频文件太大时,它有时会崩溃。有没有办法以较小的位或闪存或任何其他方式存储数据,使我能够在不超过 iPhone 上最大 RAM 的情况下处理大文件。
例如,我使用 NSData 的一件事是连接 2 个文件,如下所示:
[data1 appendData: data2];
谢谢
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 when 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个(从此SO问题):
然后,您可以将每个较小的块存储在某处,并在连接后稍后对其执行您想要的操作。
这一切都未经我的测试,但看起来很合理。
Try this (from this SO question):
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.
当然。这个问题比 NSData 更根本 - 您通常不会将一组音频文件(完整)加载到内存中,特别是当设备内存很少时。即使在 osx 上,加载整个文件也是不常见的,因为您可以在 osx 上拥有足够的内存(除非已知文件非常小)。这就是为什么音频文件 API 允许您按块读取和写入(参考:
ExtAudioFileRead
或AudioFileReadPackets
)。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
orAudioFileReadPackets
).