我正在开发一个应用程序,其中我正在从我们的服务器下载视频并从中进行实时照片。我使用以下代码从URL下载。但是,使用afnetworking的downloadTaskWithRequest
花费了很多时间。有什么方法可以更快地完成此过程?
if (![[NSFileManager defaultManager] fileExistsAtPath:fileVdPath])
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = VideoUrl;
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSLog(@"vdodownload");
NSURL *urlFilepath = [NSURL fileURLWithPath:fileVdPath];
return urlFilepath;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if ([[NSFileManager defaultManager] fileExistsAtPath:fileImgPath])
{
NSURL *photopathURL = [[NSURL alloc] initFileURLWithPath:fileImgPath];
NSURL *videoPathURL = [[NSURL alloc] initFileURLWithPath:fileVdPath];
NSLog(@"Video%d",index);
[self CreateLivePhoto:photopathURL videoUrl:videoPathURL index:index];
}
}];
[downloadTask resume];
}
dataWithContentsOfURL
可能更快
示例用法:(不要忘记将 NSPhotoLibraryAddUsageDescription
添加到您的info.plist中,以便将摄像机滚动零件保存到可行;您可以在验证速度更改(如果存在的话(后根据需要将其删除并使用它(
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *documentsDirectory ;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];
[urlData writeToFile:filePath atomically:YES];
UISaveVideoAtPathToSavedPhotosAlbum(filePath, nil, nil, nil);//add NSPhotoLibraryAddUsageDescription info.plist
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Grabbed!"
message:@"We grabbed this video and saved it to your camera roll, feel free to use now!."
delegate:self
cancelButtonTitle:@"Thanks"
otherButtonTitles:nil];
[alert show];
}