4

照片应用程序如何在后台从 CameraRoll 上传所有内容?

我需要根据日期范围在后台上传 100 张照片。我的应用程序目前正在使用NSURLSession以下代码(我认为......)但要使其正常工作,我的任务调度程序必须将 JPG 复制到应用程序存储中的文件(请参阅:Background Upload With Stream Request Using NSUrlSession in iOS8)之前应用程序进入后台。对于 100 多张照片,这需要太多时间和存储空间。

有没有办法使用“流”方法,或者NSURLSession从后台可靠地安排额外的任务?我的开发人员说,可能在 iCloud 中的 CameraRoll 照片会导致后台调度失败。

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSString *identifier = task.originalRequest.allHTTPHeaderFields[@"X-Image-Identifier"];

NSDictionary *d = [self sessionInfosDictionary];
NSURLSessionTaskInfo *info = d[identifier];
double p = (double)totalBytesSent/(double)totalBytesExpectedToSend;
info.progress = p;
[self saveSessionInfos:d];

for (id<PhotosUploaderDelegate>delegate in _delegates) {
    if ([delegate respondsToSelector:@selector(photoUploader:didUploadDataForAssetWithIdentifier:totalBytesSent:totalBytesExpectedToSend:)]) {
        [delegate photoUploader:self didUploadDataForAssetWithIdentifier:identifier totalBytesSent:totalBytesSent totalBytesExpectedToSend:totalBytesExpectedToSend];
    }
  }
}
4

2 回答 2

1

我还没有尝试过,但也许您可以复制文件并在后台会话回调中启动新的上传任务?这样您就可以一次复制和上传一个文件。

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
   // copy file and schedule new upload task
}
于 2015-03-28T22:49:37.350 回答
1

任务不简单,还有很多工作要做。

从 [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:] 和 +[NSURLSession uploadTaskWith...] 方法开始。

您会看到棘手的部分是从上传错误中恢复。您需要通过检查 -[NSURLSession getTasksWithCompletionHandler:] 来跟踪应用程序中的每个后台上传。但首先从头开始,后台会话配置和上传任务。

于 2015-03-26T08:56:19.473 回答