objective C QLPreviewController does not display i

2019-08-24 06:55发布

问题:

i used Afnetworking to download image file, and display it using QlViewController. Here is my image:

Here is my code to download and display image:

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"NSProgress: %@", uploadProgress);
//        [SVProgressHUD showProgress:uploadProgress.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) {
        [SVProgressHUD dismiss];
        if(!error){
            filePartToView = filePath;
            QLPreviewController *previewController=[[QLPreviewController alloc]init];
            previewController.delegate=self;
            previewController.dataSource=self;
            [self.navigationController presentViewController:previewController animated:YES completion:nil];
            [previewController.navigationItem setRightBarButtonItem:nil];
        }else{
            [Utilities showAlertDialog:[NSString stringWithFormat:@"Download file fail: \%@", [error localizedDescription]] atViewController:self];
        }
    }];
    [downloadTask resume];

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}

- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    return filePartToView;
}

When debug this line:

filePartToView = filePath;

I got:

po filePath
file:///Users/macos/Library/Developer/CoreSimulator/Devices/90966F81-A0D1-4CCF-BA9B-B306AAB62993/data/Containers/Data/Application/676B50E3-E364-4CDB-85DC-BF90D4C8FAED/Documents/IMG_0002.JPG

But when present in QLPreviewController:

I dont know why it happened, can anyone help my problem?