NSURLSession 和后台流上传 [英] NSURLSession and stream upload in background

查看:34
本文介绍了NSURLSession 和后台流上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 NSURLSession 将照片从资源库上传到服务器时遇到了一些问题.

I have some problems with using NSURLSession to upload photos from Asset Library to the server.

一开始NSURLSession不支持流式上传.我在尝试使用它时遇到了异常:

At first NSURLSession doesn't support streaming upload. I got an exception when trying to using that:

@property (nonatomic, strong) NSURLSession *uploadSession;

...

_uploadSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration
                backgroundSessionConfiguration:kUploadBackgroundURLSessionIdentifier] delegate:self delegateQueue:nil];

...

NSURLSessionUploadTask *task = [self.uploadSession uploadTaskWithStreamedRequest:URLRequest];

这是一个例外:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks in background sessions must be from a file'

这真的很奇怪,因为 Apple 的手册没有包含有关仅将 uploadTaskWithRequest:fromFile: 用于后台会话的任何信息.如果我想从资源库上传非常大的视频文件怎么办?我应该先将它保存到我的 tmp 目录吗?

That's really strange because Apple's manual doesn't contain any information about using only uploadTaskWithRequest:fromFile: for background session. What if I would like to upload really huge video file from Asset Library? Should I save it previously to my tmp directory?

看起来唯一的原因是无论如何都要使用 uploadTaskWithRequest:fromFile: ,对吧?但是我有一个问题,如果上传过程被中断并开始在后台上传下一部分,服务器如何知道现在正在上传文件的哪一部分?

Looks like the only reason is to use uploadTaskWithRequest:fromFile: anyway, right? But then I have a question how server get to know what's part of the file is uploading right now if uploading process was interrupted and started to upload next part in background?

我应该为此做点什么吗?以前,如果我想继续上传之前启动的部分文件,我会在 URL 请求中使用 Content-Range.现在我不能这样做 - 我必须在创建上传任务之前创建一个 URL 请求,并且看起来 NSURLSession 必须为我自动执行类似的操作?

Should I manage something for that? Previously I used Content-Range for that in URL request if I wanted to continue upload part of the file which was started previously. Now I can't do that - I have to create an URL Request before creating upload task and looks like NSURLSession have to do something like that automatically for me?

有没有人做过类似的事情?谢谢

Does anyone do something like that already? Thanks

推荐答案

转换成NSData并复制写入app文件夹

Convert to NSData and copy and write in app folder

ALAsset *asset = [cameraRollUploadImages objectAtIndex:startCount];
ALAssetRepresentation *representation = [asset defaultRepresentation];

// create a buffer to hold the data for the asset's image
uint8_t *buffer = (Byte *)malloc(representation.size);// copy the data from the asset into the buffer
NSUInteger length = [representation getBytes:buffer 
                                  fromOffset:0 
                                      length:representation.size 
                                       error:nil];

// convert the buffer into a NSData object, free the buffer after
NSData *image = [[NSData alloc] initWithBytesNoCopy:buffer 
                                             length:representation.size
                                       freeWhenDone:YES];

这篇关于NSURLSession 和后台流上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆