2016-08-18 107 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

相關問題