下载并将视频保存到相机胶卷

时间:2015-07-08 07:13:57

标签: ios swift video alamofire camera-roll

根据我所阅读的所有示例和文档,这似乎应该是非常直接的,但我仍然无法通过某些奇怪的原因使其工作。

我正在使用Alamofire Frame工作从Instagram下载视频。下载完成后,我想将视频保存到相机胶卷。以下是我下载视频并保存到磁盘的代码:

let destination: (NSURL, NSHTTPURLResponse) -> (NSURL) = {
            (temporaryURL, response) in

            if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {

                let finalPath = directoryURL.URLByAppendingPathComponent("\(Scripts.dateToString2(date: NSDate())).\(response.suggestedFilename!)")
                InstagramEngine.downloadMediaPath = finalPath

                Scripts.log("Final Path >> \(finalPath)")

                return finalPath
            }

            return temporaryURL
        }

        let request = Alamofire.download(.GET, self.videoURL, destination)

        request.response { _, response, data, error in

                NSNotificationCenter.defaultCenter().postNotificationName(kMediaDownloadComplete, object: nil)

        }

下载完成后,会触发通知,调用此功能将其保存到相机胶卷:

UISaveVideoAtPathToSavedPhotosAlbum(InstagramEngine.downloadMediaPath.URLString, self, Selector("video:didFinishSavingWithError:contextInfo:"), nil)

根据我的日志语句调用所有内容,并且没有发生错误。甚至还为UISaveVideoAtPathToSavedPhotosAlbum成功调用了didFinishSavingWithError,我确认没有发现错误。但是当我去检查相机胶卷时,我仍然看不到那里保存的视频。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

不幸的是,有一个与UISaveVideoAtPathToSavedPhotosAlbum和格式mp4相关的错误,这是Instagram使用的格式。

有一个名为UIVideoAtPathIsCompatibleWithSavedPhotosAlbum的辅助方法可帮助指示视频是否与方法UISaveVideoAtPathToSavedPhotosAlbum兼容。对于从Instagram下载的视频,这将返回false

幸运的是,仍然可以将视频存储到相机胶卷中。这可以使用ALAssetsLibrary。我已尝试使用您的示例代码并对其进行调整以使用ALAssetsLibrary,希望这可以帮助您实现它。

import AssetsLibrary

...
...

func downloadVideoToCameraRoll() {

    // Local variable pointing to the local file path for the downloaded video
    var localFileUrl: String?

    // A closure for generating the local file path for the downloaded video. This will be pointing to the Documents directory with a unique UDID file name.
    let destination: (NSURL, NSHTTPURLResponse) -> (NSURL) = {
        (temporaryURL, response) in

        if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
            let finalPath = directoryURL.URLByAppendingPathComponent("\(NSUUID()).\(response.suggestedFilename!)")
            localFileUrl = finalPath.absoluteString
            return finalPath
        }

        return temporaryURL
    }

    // The media post which should be downloaded
    let postURL = NSURL(string: "https://api.instagram.com/v1/media/" + "952201134785549382_250131908" + "?access_token=" + InstagramEngine.sharedEngine().accessToken)!

    // Then some magic happens that turns the postURL into the videoURL, which is the actual url of the video media:
    let videoURL = NSURL(string: "https://scontent.cdninstagram.com/hphotos-xfp1/t50.2886-16/11104555_1603400416544760_416259564_s.mp4")!

    // Download starts
    let request = Alamofire.download(.GET, videoURL, destination)

    // Completion handler for the download
    request.response { (request, response, data, error) -> Void in
        if let path = localFileUrl {
            let isVideoCompatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)
            println("bool: \(isVideoCompatible)") // This logs out "bool: false"

            let library = ALAssetsLibrary()

            library.writeVideoAtPathToSavedPhotosAlbum(NSURL(string: path), completionBlock: { (url, error) -> Void in
                // Done! Go check your camera roll
            })
        }
    }
}

答案 1 :(得分:0)

以下是使用Alamofire下载和保存视频的方法。代码在Swift 4中。请注意,我们需要使用Photos框架,因为AssetsLibrary已被弃用

import UIKit
import Alamofire
import Photos

/// Download and save video to camera roll
final class VideoFetcher {
  private let fileManager: FileManager

  init(fileManager: FileManager = FileManager.default) {
    self.fileManager = fileManager
  }

  func downloadAndSave(videoUrl: URL, completion: @escaping (Bool) -> Void) {
    let destination: (URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions) = {
      tempUrl, response in

      let option = DownloadRequest.DownloadOptions()
      let finalUrl = tempUrl.deletingPathExtension().appendingPathExtension(videoUrl.pathExtension)
      return (finalUrl, option)
    }

    Alamofire.download(videoUrl, to: destination)
      .response(completionHandler: { [weak self] response in
        guard response.error == nil,
          let destinationUrl = response.destinationURL else {
            completion(false)
            return
        }

        self?.save(videoFileUrl: destinationUrl, completion: completion)
      })
  }

  private func save(videoFileUrl: URL, completion: @escaping (Bool) -> Void) {
    PHPhotoLibrary.shared().performChanges({
      PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoFileUrl)
    }, completionHandler: { succeeded, error in
      guard error == nil, succeeded else {
        completion(false)
        return
      }

      completion(true)
    })
  }
}