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.
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.
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.
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.
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.
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With