我通常不喜欢问问题,但这次没有找到解决我问题的方法,所以我不得不问你如何解决我的问题。
我创建了一个对象OffLineModeBrain用于在应用程序正常运行时在后台下载大量图像,我编写代码,一切正常我只是内存有问题,我已经尝试了每个解决方案来一次次释放它但没有结果。基本上在我的 OffLineModeBrain中,我有一个方法可以做到这一点并加载一个进度条,这是代码:
-(void)downloadAndSaveByArray:(NSArray *)array{
//PATH
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSCachesDirectory,
NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *cachesImageFolderPath = [cachesDirectory stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
dispatch_queue_t queueImages = dispatch_queue_create("Images", NULL);
dispatch_queue_t queueDownload = dispatch_queue_create("Download", NULL);
//dispatch_queue_t queueStore = dispatch_queue_create("Store", NULL);
int total = array.count;
__block int progess = 0;
__weak OffLineModeBrain *wself = self;
dispatch_sync(queueImages, ^{
for ( NSString *image in array) {
if (_stop) {
//
NSLog(@"stop");
_stop = NO;
_downloading = NO;
break;
}else{
//CHECK IMAGE IN LOCAL
BOOL success = [fileManager fileExistsAtPath:
[cachesImageFolderPath stringByAppendingPathComponent:
[NSString stringWithFormat:@"/%@", [[image MD5String] lowercaseString]]]];
if (!success) {
dispatch_sync(queueDownload, ^{
if (!_stop) {
//DOWNLOAD AND SAVE
NSError *error;
NSURL *url = [NSURL URLWithString:image];
_urlData = [NSData dataWithContentsOfURL:url] ;
NSLog(@"Download");
if (_urlData)
{
//dispatch_sync(queueStore, ^{
NSString *filePath = [NSString stringWithFormat:@"%@/%@", cachesImageFolderPath, [image MD5String]];
[_urlData writeToFile:filePath atomically:NO];
NSLog(@"Stored");
//RETURN PROGRESS
progess = progess + 1;
[wself.delegate downloadImagesProgress:progess ofTotal:total];
//});
}else{
NSLog(@"Download Images erroe: %@", error);
//RETURN PROGRESS
progess = progess + 1;
[wself.delegate downloadImagesProgress:progess ofTotal:total];
}
//dsf
}else{
NSLog(@"stop");
progess = progess + 1;
if (progess == total) {
_stop = NO;
}
_downloading = NO;
}
});
}else{
NSLog(@"Local");
//RETURN PROGRESS
progess = progess + 1;
[wself.delegate downloadImagesProgress:progess ofTotal:total];
}
}
}//FOR IMAGES ENDED
});
}
这 downloadImagesProgress: ofTotal:
只是每次更新加载栏的一种方法。当我开始一个接一个地下载图像时问题就开始了,然后它们没有释放并且仍然在堆中直到循环结束,如果在结束它们之前停止循环也是释放,问题是当循环运行它们时下载和存储后没有释放并且仍然在堆中,我在仪器中可以看到CFData(存储)每次都在增长,并且在循环结束时将被释放。