如何获取拍摄的最新视频的URL? 我不想获取所有视频,只是最后一个。 以下是我如何保存视频的代码,正在创建一个保存功能,该功能可以帮助我将视频保存到自定义相册,然后调用此功能:
// Saving function
func saveVideo(vurl: URL) {
if assetCollection == nil {
return // If there was an error upstream, skip the save.
print("assetCollection = nil")
}
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: vurl)
let assetPlaceholder = assetChangeRequest!.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
albumChangeRequest!.addAssets([assetPlaceholder] as NSFastEnumeration)
print("TEST----------: \(albumChangeRequest!)")
}, completionHandler: nil)
}
// Saving proccess
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Got a video")
if let pickedVideo:URL = info[UIImagePickerControllerMediaURL] as? URL {
// Save video to the custom photo album
print(pickedVideo)
if mainrepo == "" {
print("saving")
CustomPhotoAlbum.sharedInstance.saveVideo(vurl: pickedVideo)
// saving video url to core data
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let saver = NSEntityDescription.insertNewObject(forEntityName: "Repository", into: context)
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let result = formatter.string(from: date)
print("time - \(hour):\(minutes)")
print("date - \(result)")
saver.setValue("\(pickedVideo)", forKey: "url")
saver.setValue("\(hour):\(minutes)", forKey: "time")
saver.setValue(result, forKey: "date")
saver.setValue("...", forKey: "comment")
appDelegate.saveContext()
print("saved")
}
}