我以为我已经弄清楚了,但我就是无法让它发挥作用。我有一个在数组中的每个 URL 上调用的方法。此方法有一个图片的 URL,应下载到应用程序支持文件夹中的特定路径以供离线使用。但也许我误解了 AFNetwork 库中的方法。我的方法如下所示:
- (void) downloadImageInBackground:(NSDictionary *)args{
@autoreleasepool {
NSString *photourl = [args objectForKey:@"photoUrl"];
NSString *articleID = [args objectForKey:@"articleID"];
NSString *guideName = [args objectForKey:@"guideName"];
NSNumber *totalNumberOfImages = [args objectForKey:@"totalNumberOfImages"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photourl]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = [NSInputStream inputStreamWithURL:[NSURL URLWithString:photourl]];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
DLog(@"PROBLEMS_AF");
}];
DLog(@"URL_PHOTOURL: %@", photourl);
DLog(@"indexSet: %@", operation.hasAcceptableStatusCode);
[operation response];
NSData *data = [args objectForKey:@"data"];
NSString *path;
path = [NSMutableString stringWithFormat:@"%@/Library/Application Support/Guides", NSHomeDirectory()];
path = [path stringByAppendingPathComponent:guideName];
NSString *guidePath = path;
path = [path stringByAppendingPathComponent:photourl];
if ([[NSFileManager defaultManager] fileExistsAtPath:guidePath]){
[[NSFileManager defaultManager] createFileAtPath:path
contents:data
attributes:nil];
}
DLog(@"path: %@", path);
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation start];
DLog(@"isExecuting: %d",[operation isExecuting]);
DLog(@"IS_FINISHED: %d",[operation isFinished]);
}
}
PhotoURL 是我要下载的图像的直接链接。
由于对所有图像调用此方法,因此多次调用所有日志,并且似乎是正确的。