QLPreviewController iOS Download File from Remote Server URL

What approach should be used when downloading files from a remote server to QLPreviewController? When do we need to upload files using my server API to upload them to QLPreviewController. I am adding a QLPreviewController as a subview to my current view. I can use the datasource method to call the call to download the file from the server.

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index

But after downloading the file, I need to restart the QLPreviewController, where it should be done. For images, I would like to browse the gallery to scroll through the images downloaded from the server. Can anyone tell me any tutorial to download images from the remote server url.

+4
source share
1 answer

To show any file supporting QLPreviewControllerurl must be a url file.

(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index

It always returns fileURL- if you use any other URL, it will crash. After the download is completed, save the file in the document directory, and then click to preview.

- (void)saveFileInDocDirectoryWithFileName:(NSString *)title{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath_ = [paths objectAtIndex:0];
    NSString *filePath = [ docPath_ stringByAppendingPathComponent:title];

    self.fileURL = [NSURL fileURLWithPath:filePath];
        [self pushToPreViewWithURL:fileURL];
}


- (void)pushToPreViewWithURL:(NSURL *)filePathURL{     
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    previewController.dataSource = self;
    previewController.delegate = self;
    // start previewing the document at the current section index

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

        [self.navigationController pushViewController:previewController animated:YES];

    }]; 
}

Then in the delegate method, return fileURL:

 - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx {
     return self.fileURL;
 }
+2
source

Source: https://habr.com/ru/post/1539282/


All Articles