如何从网址下载视频并保存在图库中?

时间:2016-08-18 13:01:56

标签: ios swift avplayer apple-tv

我有viewSettingsViewController,我想从文本域中的url下载电影。

以下是app的屏幕:

enter image description here

以下是我的代码:

导入UIKit 导入AVFoundation 导入AVKit class SettingsViewController:UIViewController {

@IBOutlet var urlLabel: UITextField!
let PlayerController = AVPlayerViewController()
var Player:AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()



    let videoUrl:NSURL = NSData(contentsOfURL:urllabel)

    if let url = videoURL {
        self.Player = AVPlayer(URL: url)
        self.PlayerController.player = self.Player
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func downloadButton(sender: AnyObject) {

    self.presentViewController(self.PlayerController, animated: true) { 
        self.PlayerController.player?.play()
    }
}

}

有人建议如何制作这个应用程序吗?

1 个答案:

答案 0 :(得分:-1)

所有这一切都应该在另一个线程中完成(非UI线程)。

这是最基本的下载方式,如果您想要更好地满足您的需求,请尝试使用 AFNNetworking 下载。

#import <AssetsLibrary/AssetsLibrary.h>

NSData *data = [NSData dataWithContentsOfURL:url];

// Write it to cache directory
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
[data writeToFile:path atomically:YES];


// After that use this path to save it to PhotoLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {

    if (error) {
        NSLog(@"%@", error.description);
    }else {
        NSLog(@"Done :)");
    }
}];