2016-08-18 3 views
0

viewSettingsViewController가 있는데, 텍스트 필드에있는 URL에서 영화를 다운로드하고 싶습니다. 다음은 URL에서 비디오를 다운로드하고 갤러리에 저장하는 방법?

enter image description here

내 코드입니다 : 아래

는 응용 프로그램의 화면입니다

수입 UIKit 가져 오기 클래스 SettingsViewController AVKit 수입 AVFoundation :의 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

관련 문제