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

查看:178
本文介绍了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'

这真是奇怪,因为苹果的手册不包含有关使用仅 uploadTaskWithRequest的任何信息:FROMFILE:背景会话。如果我想做些什么,从资源库上传真的庞大的视频文件?我要救它pviously $ P $到我的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?

我应该管理的东西是什么? previously我用的内容范围,在URL请求,如果我想继续这是pviously开始$ P $文件上传一部分。现在,我不能做到这一点 - 我有创建上传任务之前创建一个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的,复制并应用文件夹的写

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天全站免登陆