Afnetworking 3.0 预计写入的总字节数 return -1
Afnetworking 3.0 total Bytes Expected To Write return -1
我想配置进度条,但我的 url 无法提供图像的总大小。我遵循本教程 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];
如果 totalBytesExpectedToWrite
为 -1,则说明您的服务器配置存在一些问题,因为它没有响应图像的总大小。
没有总大小就不可能做正确的进度条,你只能显示一些 activity 指示器(这只是表明它正在发生,但不会在完成时给出反馈).
我建议查看您的服务器设置(尤其是要以正确的方式设置 Content-Length
header)。
试试这个:
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];
我想配置进度条,但我的 url 无法提供图像的总大小。我遵循本教程 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];
如果 totalBytesExpectedToWrite
为 -1,则说明您的服务器配置存在一些问题,因为它没有响应图像的总大小。
没有总大小就不可能做正确的进度条,你只能显示一些 activity 指示器(这只是表明它正在发生,但不会在完成时给出反馈).
我建议查看您的服务器设置(尤其是要以正确的方式设置 Content-Length
header)。
试试这个:
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];