81 lines
2.8 KiB
Swift
81 lines
2.8 KiB
Swift
//
|
|
// testVideoVC.swift
|
|
// wallpaper_project
|
|
|
|
|
|
import UIKit
|
|
import AVFoundation
|
|
import Photos
|
|
import PhotosUI
|
|
|
|
class testVideoVC: WA_RootVC {
|
|
var livePhotoOutput: PHLivePhotoView!
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
convertMovToLivePhoto()
|
|
}
|
|
func convertMovToLivePhoto() {
|
|
// Load the MOV file
|
|
guard let videoURL = Bundle.main.url(forResource: "a", withExtension: "mov") else {
|
|
print("Video file not found.")
|
|
return
|
|
}
|
|
|
|
// Load the photo file
|
|
guard let photoURL = Bundle.main.url(forResource: "g", withExtension: "jpg") else {
|
|
print("Photo file not found.")
|
|
return
|
|
}
|
|
|
|
// Check if the file exists at the given URLs
|
|
guard FileManager.default.fileExists(atPath: videoURL.path) && FileManager.default.fileExists(atPath: photoURL.path) else {
|
|
print("Files not found at specified URLs.")
|
|
return
|
|
}
|
|
|
|
// Perform changes to save the Live Photo
|
|
PHPhotoLibrary.shared().performChanges({
|
|
let creationRequest = PHAssetCreationRequest.forAsset()
|
|
let options = PHAssetResourceCreationOptions()
|
|
options.shouldMoveFile = true
|
|
creationRequest.addResource(with: .photo, fileURL: photoURL, options: nil)
|
|
creationRequest.addResource(with: .pairedVideo, fileURL: videoURL, options: options)
|
|
}) { [weak self] success, error in
|
|
if success {
|
|
print("Live Photo created successfully.")
|
|
// Display the Live Photo
|
|
self?.displayLivePhoto()
|
|
} else {
|
|
print("Failed to create Live Photo:", error?.localizedDescription ?? "Unknown error")
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func displayLivePhoto() {
|
|
let options = PHFetchOptions()
|
|
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
|
|
|
|
let fetchResult = PHAsset.fetchAssets(with: .image, options: options)
|
|
if let lastAsset = fetchResult.firstObject {
|
|
PHImageManager.default().requestLivePhoto(for: lastAsset, targetSize: view.frame.size, contentMode: .aspectFill, options: nil) { [weak self] (livePhoto, _) in
|
|
DispatchQueue.main.async {
|
|
guard let livePhoto = livePhoto else {
|
|
print("Failed to get Live Photo")
|
|
return
|
|
}
|
|
|
|
// Display the Live Photo
|
|
self?.livePhotoOutput = PHLivePhotoView(frame: self?.view.bounds ?? CGRect.zero)
|
|
self?.livePhotoOutput.livePhoto = livePhoto
|
|
self?.view.addSubview(self?.livePhotoOutput ?? UIView())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|