当我使用AFNetworking 2时,我可以像AFMTPRequestOperation一样取得进展:
del.sh: line 4: [[263_V01_C00_R000_TEx_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C00_R079_TEx_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C01_R000_TEy_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C01_R079_TEy_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C02_R000_THx_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C02_R079_THx_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C02_R081_THx_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C03_R000_THy_BL_4096H.ats: command not found
del.sh: line 4: [[263_V01_C03_R079_THy_BL_4096H.ats: command not found
我使用NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:aURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:_timeoutInterval];
AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
imageRequestOperation.responseSerializer = [AFImageResponseSerializer serializer];
__weak AFHTTPRequestOperation *imageRequestOperationForBlock = imageRequestOperation;
[imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// ...
}];
[imageRequestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
if (progress) {
progress((float)totalBytesRead / totalBytesExpectedToRead);
}
}];
来获取数据,但我不知道如何在AFNetworking 3.0中获得下载进度。
答案 0 :(得分:3)
评论中的链接是误导性的(NSURLSessionDownloadTask
)。对不起。
下面的代码应该可以使用。
假设:此代码放在AFHTTPSessionManager
子类中,声明了NSURLSessionDataTask *testTask
ivar。它应该很容易根据需要进行修改。
- (void)test
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://eoimages.gsfc.nasa.gov/images/imagerecords/78000/78797/nwpassage_tmo_2012199_lrg.jpg"]];
testTask = [self dataTaskWithRequest:request
completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error)
{
//...
NSLog(@"error");
}
else
{
//...
UIImage *result = responseObject;
NSLog(@"Success: %@",NSStringFromCGSize(result.size));
}
}];
[self setDataTaskDidReceiveDataBlock:^(NSURLSession *session,
NSURLSessionDataTask *dataTask,
NSData *data)
{
if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown)
return;
if (dataTask != testTask)
return;
NSUInteger code = [(NSHTTPURLResponse *)dataTask.response statusCode];
if (!(code> 199 && code < 400))
return;
long long bytesReceived = [dataTask countOfBytesReceived];
long long bytesTotal = [dataTask countOfBytesExpectedToReceive];
NSLog(@"... %lld/%lld",
bytesReceived,
bytesTotal);
}];
[testTask resume];
}