我从 PhotoLibrary 将大视频资产上传到服务器时遇到问题。
我按照此处所述获取资产数据,将视频导出到本地文档,然后上传。
但是当我上传一个大视频(2分钟,大约300Mb或更大)时,这种方法会导致崩溃,我没有任何理由也没有任何信息。
我webDAV
用来上传文件是这样的:
// Set up credentials
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:host
port:80
protocol:@"http"
realm:@" webDAV"
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials forProtectionSpace:space];
[space release];
// Create the request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"PUT"];
[request setValue:[self mimetypeForFile:self.filepath] forHTTPHeaderField:@"Content-Type"];
NSNumber *contentLength = (NSNumber *) [[[NSFileManager defaultManager]
attributesOfItemAtPath:self.filepath error:NULL]
objectForKey:NSFileSize];
[request setValue:[contentLength description] forHTTPHeaderField:@"Content-Length"];
if (self.useStreaming)
{
if (self.currentFileStream!=nil)
{
[self.currentFileStream close], self.currentFileStream = nil;
}
self.currentFileStream = [NSInputStream inputStreamWithFileAtPath:self.filepath];
if (currentFileStream!=nil)
{
[request setHTTPBodyStream:currentFileStream];
}
else
{
[request setHTTPBody:[NSData dataWithContentsOfFile:self.filepath]];
}
}
else
{
[request setHTTPBody:[NSData dataWithContentsOfFile:self.filepath]];
}
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (self.currentConnection!=nil)
{
self.currentConnection = nil;
}
self.currentConnection = conn;
[conn release];
[self.currentConnection start];
当代码到达这一行时:
self.currentFileStream = [NSInputStream inputStreamWithFileAtPath:self.filepath];
[request setHTTPBodyStream:currentFileStream];
或者:
[request setHTTPBody:[NSData dataWithContentsOfFile:self.filepath]];
它崩溃了。
你有什么建议吗?
谢谢。
=========================
setHTTPBody:
编辑:它在OR 处崩溃setHTTPBodyStream:
所以我认为这是关于内存泄漏之类的。
=================
EDIT2:现在我决定压缩视频,我通过当前方法得到的视频数据太大(超过300mb),但我发现使用UIImagePickerController
选择相同的视频,它只有30mb;所以压缩是有帮助的;我会尝试UIVideoEditorController
,很快就会发布我的结果;