我在使用时遇到一些问题NSURLSession
将照片从资源库上传到服务器。
首先NSURLSession
不支持流式上传。我在尝试使用它时遇到了异常:
@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:
用于后台会话。如果我想从资源库上传非常大的视频文件怎么办?我应该事先将其保存到我的 tmp 目录中吗?
看起来唯一的原因是使用uploadTaskWithRequest:fromFile:
无论如何,对吧?但是我有一个问题,如果上传过程被中断并开始在后台上传下一部分,服务器如何知道现在正在上传文件的哪些部分?
我应该为此做点什么吗?以前,如果我想继续上传之前启动的部分文件,我会在 URL 请求中使用 Content-Range。现在我不能这样做 - 我必须在创建上传任务之前创建一个 URL 请求,看起来像NSURLSession
必须自动为我做类似的事情吗?
有人已经做了类似的事情吗?谢谢
转换为NSData并复制并写入app文件夹
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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)