我一直在谷歌搜索无济于事,希望您对此有所帮助。
我尝试使用 AFHTTPRequestOperation 流(通过设置输出流)下载图像。它下载文件,没问题。但是进度条显示不正确,因为 totalBytesExpectedToRead 总是返回-1,并且只有在下载完成时才返回正确的值。
这是流媒体的本质吗?还是我做错了什么?
我的代码如下。
提前致谢!
(void)invokeAsynchronousSTREAMING:(NSString*)path locationToSave:(NSString*)locationToSave 参数:(NSDictionary*)paramDict callId:(NSString*)callId {
NSMutableURLRequest *request = [[AFServerAPIClient sharedClient] requestWithMethod:@"GET"
path:path
parameters:paramDict];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
if(locationToSave!=nil) {
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:locationToSave append:NO];
}
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//DO SOMETHING
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//DO SOMETHING
}
];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"invokeAsyncronousSTREAMING - Received %lld of %lld bytes", totalBytesRead, totalBytesExpectedToRead);
//DO SOMETHING
}];
[operation start];
}//结束调用AsyncStreaming