我想配置进度条,但我的网址无法给出我图像的总大小。我按照本教程http://tarikfayad.com/afnetworking-downloading-files-with-progress/
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//Most URLs I come across are in string format so to convert them into an NSURL and then instantiate the actual request
NSURL *formattedURL = [NSURL URLWithString:@"http://www.wallpapers13.com/wp-content/uploads/2016/04/Sunset-Large-Chinese-wall-Desktop-Wallpaper-HD-5250x3344.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL];
[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDownloadTask * _Nonnull downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
//CGFloat w = totalBytesWritten;
CGFloat t = totalBytesExpectedToWrite;
//CGFloat percentageCompleted = w/t;
NSLog(@"%f",t); // Error retun -1
}];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//
//Getting the path of the document directory
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"image1.jpg"];
//If we already have a video file saved, remove it from the phone
[self removeVideoAtPath:fullURL];
return fullURL;
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
//If there's no error, return the completion block
[self completionBlock:(NSString *)filePath];
} else {
//Otherwise return the error block
[self errorBlock:error];
}
}];
[download resume];
答案 0 :(得分:1)
如果totalBytesExpectedToWrite
为-1,那么您的服务器配置就会出现问题,因为它没有响应图片的总大小。
如果没有总大小,则无法执行正确的进度条,您只能显示一些活动指示符(仅指示其发生的事情,但在完成时不会给出反馈)。
我建议您查看服务器设置(尤其是在正确的方式设置Content-Length
标题)。
答案 1 :(得分:1)
试试这个:
NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL * URL = [NSURL URLWithString:@"http://static1.visitfinland.com/wp-content/uploads/Header_Linnunlaulu_ruska_autumn.jpg"];
NSURLRequest * request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask * downloadTask = [manager downloadTaskWithRequest:request
progress:^(NSProgress * _Nonnull downloadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
// here you will be able to set progress to your progress view
[yourProgressView setProgress:downloadProgress.fractionCompleted];
});
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse * response, NSURL * filePath, NSError * error) {
}];
[downloadTask resume];