ios8:如何使用NSURLSession在后台上传100张照片?自由空间问题

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

我需要根据日期范围在后台上传100张照片.我的应用程序当前正在使用带有以下代码的NSURLSession(我认为…)但是为了实现这一点,我的任务调度程序必须在应用程序进入后台之前将JPG复制到App存储中的文件(请参阅: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];
    }
  }
}

最佳答案 这项任务并非无足轻重,还有很多工作要做.

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

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

点赞