我想配置进度栏,但是我的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];

最佳答案

尝试这个:

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];

关于ios - Afnetworking 3.0预期写入的总字节数返回-1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41778548/

10-12 00:15