私はいつも質問をするのが好きではありませんが、今回は私の問題の解決策が見つからなかったので、私の問題を解決する方法をあなたに尋ねなければなりません.
アプリが正常に動作している間に、バックグラウンドで多くの画像をダウンロードするためのオブジェクト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:
、読み込みバーを毎回更新するための単なる方法です。問題は、画像を1つずつダウンロードし始めたときに始まりますが、それらは解放されず、ループが終了するまでヒープに残ります。終了する前にループを停止すると、それらも解放されます。問題は、ループがそれらを実行するときですリリースされず、ダウンロードと保存後にヒープに残っています。instrument で確認できるのは、CFData (ストア)が毎回大きくなり、ループが終了するとリリースされることです。