NSURL *url = [NSURL URLWithString:@"http://192.168.1.50/download/1.jpg"];
//NSURL *url = [NSURL URLWithString:@"http://192.168.1.50/download/3.mov"];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
// NSLog(@"%@",[res allHeaderFields]);
self.receiveData = [NSMutableData data];
self.sumLength=response.expectedContentLength;
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receiveData appendData:data];
self.currentLength+=data.length;
double progress=(double)self.currentLength/self.sumLength;
self.progress.progress=progress;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
NSString *fileName=[ourDocumentPath stringByAppendingPathComponent:@"1.mov"];
if ([self.receiveData writeToURL:[NSURL fileURLWithPath:fileName] atomically:YES])
{
NSLog(@"OK Sucess");
}else
{
NSLog(@"No Fail");
}
}
我使用这种方法将 jpg 和 mov 下载到文档目录。但无法播放和使用 photolibary 中的 [PHAssetResourceManager defaultManager] writeDataForAssetResource 来保存相同的目录并加载正常。
2016-03-07 19:22:28.854 TestJiri[8641:5589436] /var/mobile/Containers/Data/Application/D100284B-74B2-41E1-B643-7B3EF71F99DE/Documents/1.jpg
2016-03-07 19:22:28.854 TestJiri[8641:5589436] /var/mobile/Containers/Data/Application/D100284B-74B2-41E1-B643-7B3EF71F99DE/Documents/1.mov
2016-03-07 19:22:28.899 TestJiri[8641:5597595] Error: Invalid video metadata
如何加载它们?谢谢。