Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Video from a Live Photo in iOS

I'm trying to figure it out, but can't find any useful information. I only found this:

PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes, 
toFile: fileURL, options: nil, completionHandler: 
{
     // Video file has been written to path specified via fileURL
}

but I'm ashamed to say I have no idea how to play it out. I've created a UIImagePickerController and loaded an Image from the Camera Roll.

like image 633
Benny Avatar asked Dec 19 '16 13:12

Benny


People also ask

Can you turn a live Photo into video?

You can also convert the Live Photo to a video clip in the Share menu. Just click the share icon and scroll down to the Save as Video option.

Can you save a live Photo as a video iPhone?

In the Photos app, open the Live Photo you want to save as a video and tap the share button. Select Save as Video from the list of options. The Live Photo will save as a video immediately.

How do I capture a still from a live Photo?

Open the Live Photo and tap Edit. Tap the Live Photos button . Move the slider to change the frame. Release your finger, then tap Make Key Photo.


1 Answers

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

 let phAsset = info[.phAsset] as? PHAsset
 imagePickerController.dismiss(animated: true, completion: nil)
 let style = phAsset?.playbackStyle
  if(style != .livePhoto) {
         print("This is not a live photo")
         return
  }
  let filePath = NSTemporaryDirectory() + String(format: "%.0f", NSDate().timeIntervalSince1970) + "_.mov"
  let fileURL = NSURL(fileURLWithPath: filePath)
  let options = PHLivePhotoRequestOptions()
  options.deliveryMode = .fastFormat
  options.isNetworkAccessAllowed = true

PHImageManager.default().requestLivePhoto(for: phAsset!, targetSize: CGSize(width: 1920, height: 1080), contentMode: PHImageContentMode.default, options: options) { livePhoto, info in
        if((livePhoto) != nil) {
           let assetResources = PHAssetResource.assetResources(for: livePhoto!)
           var videoResource : PHAssetResource?
           for resources in assetResources {
               if(resources.type == .pairedVideo) {
                    videoResource = resources
                    break
               }
            }
            guard let videoResource = videoResource else {
                fatalError("video resource is nil")
            }
            PHAssetResourceManager.default().writeData(for: videoResource, toFile: fileURL as URL, options: nil) { error in                   
                let avAsset : AVAsset = AVAsset(url: fileURL as URL)
                 DispatchQueue.main.async { [self] in

                        // Whatever you do using fileURL or avAsset.

                 }
             }
                
         }
    }

}
like image 160
sagarthecoder Avatar answered Sep 27 '22 22:09

sagarthecoder