2016-08-18 105 views
0

我有viewSettingsViewController,并且我想从在textfield中找到的url下载电影。如何从网站下载视频并保存在图库中?

下面是应用程序的屏幕:

enter image description here

下面是我的代码:

进口的UIKit 进口AVFoundation 进口AVKit 类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

所有这些都应该在不同的线程中完成(非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 :)"); 
    } 
}]; 
+0

为什么你投下了?它没有工作吗?这不是你正在寻找的答案吗?请解释 – MCMatan

相关问题