gpt4 book ai didi

swift - 在照片库 Swift 中保存视频

转载 作者:行者123 更新时间:2023-12-04 10:16:13 28 4
gpt4 key购买 nike

我想弄清楚如何在我的照片库中保存视频。首先,我尝试使用 UIImagePickerController 简单地选择一个视频,并在选择后使用 UISaveVideoAtPathToSavedPhotosAlbum 将其再次保存在库中。没有多大意义,但我试图了解保存视频的工作原理。

以下代码不起作用,因为它不保存视频:

@IBAction func ChooseVideo(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self

imagePickerController.mediaTypes = ["public.movie"]
self.present(imagePickerController, animated: true, completion: nil)
}

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

let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL
print(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoURL!.path))

dismiss(animated: true, completion: {
UISaveVideoAtPathToSavedPhotosAlbum(videoURL!.path, self, #selector(self.video(_:didFinishSavingWithError:contextInfo:)), nil)
})
}

我希望你能帮助我,因为我找不到太多关于保存视频的信息。

此致,
MB

解决方案:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
// *** store the video URL returned by UIImagePickerController *** //
let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! URL

// *** load video data from URL *** //
let videoData = NSData(contentsOf: videoURL)

// *** Get documents directory path *** //
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]

// *** Append video file name *** //

print(paths)
let dataPath = paths.appending("/videoFileName.mp4")

// *** Write video file data to path *** //
videoData?.write(toFile: dataPath, atomically: false)

PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: dataPath))
}) { saved, error in
if saved {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject
// fetchResult is your latest video PHAsset
// To fetch latest image replace .video with .image
}
}
}

最佳答案

  • 使用以下函数将视频保存到文档目录
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) 
    {
    // *** store the video URL returned by UIImagePickerController *** //
    let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL

    // *** load video data from URL *** //
    let videoData = NSData(contentsOfURL: videoURL)

    // *** Get documents directory path *** //
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]

    // *** Append video file name *** //
    let dataPath = documentsDirectory.stringByAppendingPathComponent("/videoFileName.mp4")

    // *** Write video file data to path *** //
    videoData?.writeToFile(dataPath, atomically: false)
    }
  • 现在将此视频保存在照片凝胶中
    PHPhotoLibrary.shared().performChanges({
    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: Your document directory file)
    }) { saved, error in
    if saved {
    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

    let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject
    // fetchResult is your latest video PHAsset
    // To fetch latest image replace .video with .image
    }
    }

  • 之后,如果您不需要,则从文档目录中删除图像
    ,我希望它对你有用...:)

    关于swift - 在照片库 Swift 中保存视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044285/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com