1

問題: ドロップボックス アカウントからファイルをダウンロードし、クイック ルックを使用して視覚化したいと考えています。

最初の解決策:

1) Dropbox API restClient を使用します。

[[self restClient] loadFile:fullpath intoPath:finalpath];

2) ダウンロードしたら、QLPreviewController を使用してファイルをプレビューします。

このソリューションの問題は、ダウンロードをプレビューと同期する方法がわからないことです (クイックルックを使用するには、ファイルがローカルである必要があるため、最初にダウンロードする必要があります)。

私が思いついた(醜い)回避策は、アラート(「キャッシング」)を設定し、それを任意の時間(12秒、マジックナンバーとしましょう...)持続させることです。同時に、実行を 10 ~ 12 秒間一時停止します (マジック ナンバー)。

[NSThread sleepForTimeInterval:12.0f];

...そして、この時間間隔の終わりにファイルが既にダウンロードされていることを願っているので、QLPreviewController を開始できます。

これがコードです(醜い、私は知っています....):

// Define Alert
UIAlertView *downloadAlert = [[UIAlertView alloc] initWithTitle:@"caching" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil] ;

// If  file does not exist alert downloading is ongoing
if(![[NSFileManager defaultManager] fileExistsAtPath:finalpath])
{
    // Alert Popup
    [downloadAlert show];
    //[self performSelector:@selector(isExecuting) withObject:downloadAlert afterDelay:12];

}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //Here your non-main thread.
    if(![[NSFileManager defaultManager] fileExistsAtPath:finalpath])
    {
    [NSThread sleepForTimeInterval:12.0f];
    }

        dispatch_async(dispatch_get_main_queue(), ^{

        // Dismiss alert
        [downloadAlert dismissWithClickedButtonIndex: -1 animated:YES];

        //Here we return to main thread.
        // We use the QuickLook APIs directly to preview the document -
        QLPreviewController *previewController = [[QLPreviewController alloc] init];
        previewController.dataSource = self;
        previewController.delegate = self;
        // Push new viewcontroller, previewing the document
        [[self navigationController] pushViewController:previewController animated:YES];

    });
        });

(小さなファイルと高速接続で)動作しますが、最善の解決策ではありません... .

最善の解決策は、NSURLSession をドロップボックス restClient と統合して、このルーチンを使用することだと思います。

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
                                                        delegate:nil
                                                   delegateQueue:[NSOperationQueue mainQueue]];
  NSURLSessionDownloadTask *task;
  task = [session downloadTaskWithRequest:request
                        completionHandler:^(NSURL *localfile, NSURLResponse *response, NSErr or *error) {
/* yes, can do UI things directly because this is called on the main queue */ }];
  [task resume];

しかし、DropBox API でそれを使用する方法がわかりません:何か提案はありますか?

ありがとう、ドム

4

1 に答える 1

1

API が進行状況と完了について通知しているようです。

- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)destPath contentType:(NSString*)contentType metadata:(DBMetadata*)metadata;
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath;

スリープまたは gcd 呼び出しを直接行う必要はありません。ダウンロードが開始されたときにビジー状態を表示するように UI を変更し、これらを使用して進行状況と完了で UI を更新します。

于 2013-12-31T20:04:33.910 に答える