mechu911
mechu911

Reputation: 426

How to download video from url and save in gallery?

I have viewSettingsViewController, and I would like download movie from url which is found in textfield.

Below is screen of app:

enter image description here

Below is my code:

import UIKit import AVFoundation import 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()
    }
}

}

Any suggest how to make this app ?

Upvotes: 0

Views: 760

Answers (1)

MCMatan
MCMatan

Reputation: 8863

All of this should be done in a different thread (Not UI thread).

This is the most basic way to download, if you want something better to suit your needs, try using AFNNetworking download.

#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 :)");
    }
}];

Upvotes: -1

Related Questions