[英]How can I avoid keeping the contents of NSData in active memory?
My app requires data such as images and video to be sent across a network. 我的应用程序要求通过网络发送图像和视频等数据。 I managed to split the files up into smaller chunks using an acceptable amount of RAM, like so: 我设法使用可接受数量的RAM将文件分成较小的块,如下所示:
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"image.jpg"], 1.0);
NSArray *array = [data splitIntoSubdataWithLength:1000000];
-(NSArray *)splitIntoSubdataWithLength:(int)subdataLength {
NSMutableArray *array = [[NSMutableArray alloc] init];
NSUInteger dataLength = [self length];
NSUInteger offset = 0;
do {
NSUInteger thisChunkSize = dataLength - offset > subdataLength ? subdataLength : dataLength - offset;
NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[self bytes] + offset
length:thisChunkSize
freeWhenDone:NO];
offset += thisChunkSize;
[array addObject:chunk];
NSLog(@"chunk size: %i", chunk.length);
} while (offset < dataLength);
return array;
}
The problem is, if the original NSData object is 26MB, the RAM goes up to ~26MB. 问题是,如果原始NSData对象为26MB,则RAM会增加到〜26MB。 Do I need to have the entire contents of the NSData object active in memory like this, or would i be able to reduce the memory usage in any way? 我需要像这样在内存中激活NSData对象的全部内容,还是可以以任何方式减少内存使用量?
you can use a memory mapped file, and store the values temporarily on disk. 您可以使用内存映射文件,并将这些值临时存储在磁盘上。 here's a good implementation for ios with ARC. 这是带有ARC的ios的良好实现。
http://www.cimgf.com/2012/02/17/extending-nsdata-and-not-overriding-dealloc/ http://www.cimgf.com/2012/02/17/extending-nsdata-and-not-overriding-dealloc/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.