Xcode的新功能,试图制作一个下载动态壁纸的应用程序。 我使用来自github的LivePhotoDemo来获取所有代码。我的问题是,当页面加载到页面中时,它会显示实时照片,但会在我的照片库中保存它的两个副本,而我似乎无法弄清楚如何获取保存按钮来代替它(只有一个副本)
import UIKit
import Photos
import PhotosUI
import MobileCoreServices
struct FilePaths {
static let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.cachesDirectory,.userDomainMask,true)[0] as AnyObject
struct VidToLive {
static var livePath = FilePaths.documentsPath.appending("/")
}
}
class ViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//@IBOutlet var livePhotoView: PHLivePhotoView!
@IBOutlet var livePhotoView: PHLivePhotoView!
{
didSet {
loadVideoWithVideoURL(Bundle.main.url(forResource: "video", withExtension: "mov")!)
}
}
func loadVideoWithVideoURL(_ videoURL: URL) {
livePhotoView.livePhoto = nil
let asset = AVURLAsset(url: videoURL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let time = NSValue(time: CMTimeMakeWithSeconds(CMTimeGetSeconds(asset.duration)/2, preferredTimescale: asset.duration.timescale))
generator.generateCGImagesAsynchronously(forTimes: [time]) { [weak self] _, image, _, _, _ in
if let image = image, let data = UIImage(cgImage: image).pngData() {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let imageURL = urls[0].appendingPathComponent("image.jpg")
try? data.write(to: imageURL, options: [.atomic])
let image = imageURL.path
let mov = videoURL.path
let output = FilePaths.VidToLive.livePath
let assetIdentifier = UUID().uuidString
let _ = try? FileManager.default.createDirectory(atPath: output, withIntermediateDirectories: true, attributes: nil)
do {
try FileManager.default.removeItem(atPath: output + "/IMG.JPG")
try FileManager.default.removeItem(atPath: output + "/IMG.MOV")
} catch {
}
JPEG(path: image).write(output + "/IMG.JPG",
assetIdentifier: assetIdentifier)
QuickTimeMov(path: mov).write(output + "/IMG.MOV",
assetIdentifier: assetIdentifier)
//self?.livePhotoView.livePhoto = LPDLivePhoto.livePhotoWithImageURL(NSURL(fileURLWithPath: FilePaths.VidToLive.livePath.stringByAppendingString("/IMG.JPG")), videoURL: NSURL(fileURLWithPath: FilePaths.VidToLive.livePath.stringByAppendingString("/IMG.MOV")))
//self?.exportLivePhoto()
_ = DispatchQueue.main.sync {
PHLivePhoto.request(withResourceFileURLs: [ URL(fileURLWithPath: FilePaths.VidToLive.livePath + "/IMG.MOV"), URL(fileURLWithPath: FilePaths.VidToLive.livePath + "/IMG.JPG")],
placeholderImage: nil,
targetSize: self!.view.bounds.size,
contentMode: PHImageContentMode.aspectFit,
resultHandler: { (livePhoto, info) -> Void in
self?.livePhotoView.livePhoto = livePhoto
self?.exportLivePhoto()
})
}
}
}
}
@IBAction func Savephoto(_ sender: UIBarButtonItem){
}
func exportLivePhoto () {
PHPhotoLibrary.shared().performChanges({ () -> Void in
let creationRequest = PHAssetCreationRequest.forAsset()
let options = PHAssetResourceCreationOptions()
creationRequest.addResource(with: PHAssetResourceType.pairedVideo, fileURL: URL(fileURLWithPath: FilePaths.VidToLive.livePath + "/IMG.MOV"), options: options)
creationRequest.addResource(with: PHAssetResourceType.photo, fileURL: URL(fileURLWithPath: FilePaths.VidToLive.livePath + "/IMG.JPG"), options: options)
}, completionHandler: { (success, error) -> Void in
if !success {
NSLog((error?.localizedDescription)!)
}
})
}
}
我的主视图在顶部链接到IBOutlet,在底部附近将保存按钮链接到IBAction。实际上,除了几年前的HTML和C ++之外,我对编码的了解并不多。我只知道如何浏览代码并尝试理解所有内容。有人可以告诉我即时消息做错了什么,在哪里。非常感谢!
答案 0 :(得分:0)
以下是让您摆脱困境的一些方法。
问题1:如何获取保存按钮以调用exportLivePhoto():
您按钮的IBAction应该看起来像这样:
@IBAction func savePhotoTapped(_ sender: Any) {
print("in savePhotoTapped | start"
exportLivePhoto()
}
确保此操作已连接到您在情节提要中创建的按钮。
问题2:多次保存:
这些行看起来就像它们保存了jpg和mov版本的图像。如果您只想发表评论。
try FileManager.default.removeItem(atPath: output + "/IMG.JPG")
try FileManager.default.removeItem(atPath: output + "/IMG.MOV")
也请尝试清理您的代码,以便将来更易于阅读。