如何将我的应用程序中的视频保存到 iOS 中的用户照片库?
How to save a video from my app in to the users photo library in iOS?
我的 iOS 应用程序允许用户拍照或录制视频,如果他们愿意,可以将其发送并保存到他们的相册中。我已完成所有操作,但不知道如何保存视频。我用谷歌搜索了一下,但我看到的大部分代码似乎都已过时。我尝试了以下代码,但 Xcode 给我错误,例如“'UTTypeCopyPreferredTagWithClass' 无法在范围内找到”等(我已经导入了 UniformTypeIdentifiers)。我也不太确定这段代码是否真的做了任何事情。
// Write video to disk
guard let fileExtension = UTTypeCopyPreferredTagWithClass(photoEditViewController.configuration.photoEditViewControllerOptions.outputImageFileFormatUTI, kUTTagClassFilenameExtension)?.takeRetainedValue() as String?,
let filename = (ProcessInfo.processInfo.globallyUniqueString as NSString).appendingPathExtension(fileExtension) else {
return
}
let fileURL = URL(fileURLWithPath: (NSTemporaryDirectory() as NSString).appendingPathComponent(filename))
try? data.write(to: fileURL, options: [.atomic])
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: fileURL)
}) { _, _ in
// Delete video from disk
_ = try? FileManager.default.removeItem(at: fileURL)
}
能否改进此代码,或者有什么更好的方法可以用来将视频保存到照片库以供 iOS?
试试用这个把视频保存在照片库里,参考Save video to camera roll
func requestAuthorization(completion: @escaping ()->Void) {
if PHPhotoLibrary.authorizationStatus() == .notDetermined {
PHPhotoLibrary.requestAuthorization { (status) in
DispatchQueue.main.async {
completion()
}
}
} else if PHPhotoLibrary.authorizationStatus() == .authorized{
completion()
}
}
func saveVideoToAlbum(_ outputURL: URL, _ completion: ((Error?) -> Void)?) {
requestAuthorization {
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .video, fileURL: outputURL, options: nil)
}) { (result, error) in
DispatchQueue.main.async {
if let error = error {
print(error.localizedDescription)
} else {
print("Saved successfully")
}
completion?(error)
}
}
}
}
函数的使用:
self.saveVideoToAlbum(/* pass your final url to save */) { (error) in
//Do what you want
}
您可以使用 UISaveVideoAtPathToSavePhotosAlbum
将视频保存到图库
该函数将视频添加到指定路径下的用户相册中。
func UISaveVideoAtPathToSavedPhotosAlbum(_ videoPath: String,
_ completionTarget: Any?,
_ completionSelector: Selector?,
_ contextInfo: UnsafeMutableRawPointer?)
在 VideoPath 中,插入要保存的视频的路径。
此外,您可以先使用功能UIVideoAtPathCompatibleWithSavedPhotosAlbum(_:)
检查未保存的错误,看看视频是否可以存储在图库中。
有关详细信息,请参阅 apple developer site。
提供的两个答案都很好。我选择 Hailey's 作为 'correct' 之一,因为它非常适合简单地保存视频。对于将来遇到此 post 的任何人。您可以根据需要使用其中任何一种。 Schaheer 的代码处理请求权限和保存视频,而 Hailey 的代码只是在您有路径的情况下保存视频。我将上述两个答案结合起来,并将其与其他来源提供的示例代码一起使用。
对于使用 IMGLY 照片或视频编辑器 SDK 的任何其他人,我将 post 我很快使用的代码。
我的 iOS 应用程序允许用户拍照或录制视频,如果他们愿意,可以将其发送并保存到他们的相册中。我已完成所有操作,但不知道如何保存视频。我用谷歌搜索了一下,但我看到的大部分代码似乎都已过时。我尝试了以下代码,但 Xcode 给我错误,例如“'UTTypeCopyPreferredTagWithClass' 无法在范围内找到”等(我已经导入了 UniformTypeIdentifiers)。我也不太确定这段代码是否真的做了任何事情。
// Write video to disk
guard let fileExtension = UTTypeCopyPreferredTagWithClass(photoEditViewController.configuration.photoEditViewControllerOptions.outputImageFileFormatUTI, kUTTagClassFilenameExtension)?.takeRetainedValue() as String?,
let filename = (ProcessInfo.processInfo.globallyUniqueString as NSString).appendingPathExtension(fileExtension) else {
return
}
let fileURL = URL(fileURLWithPath: (NSTemporaryDirectory() as NSString).appendingPathComponent(filename))
try? data.write(to: fileURL, options: [.atomic])
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: fileURL)
}) { _, _ in
// Delete video from disk
_ = try? FileManager.default.removeItem(at: fileURL)
}
能否改进此代码,或者有什么更好的方法可以用来将视频保存到照片库以供 iOS?
试试用这个把视频保存在照片库里,参考Save video to camera roll
func requestAuthorization(completion: @escaping ()->Void) {
if PHPhotoLibrary.authorizationStatus() == .notDetermined {
PHPhotoLibrary.requestAuthorization { (status) in
DispatchQueue.main.async {
completion()
}
}
} else if PHPhotoLibrary.authorizationStatus() == .authorized{
completion()
}
}
func saveVideoToAlbum(_ outputURL: URL, _ completion: ((Error?) -> Void)?) {
requestAuthorization {
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .video, fileURL: outputURL, options: nil)
}) { (result, error) in
DispatchQueue.main.async {
if let error = error {
print(error.localizedDescription)
} else {
print("Saved successfully")
}
completion?(error)
}
}
}
}
函数的使用:
self.saveVideoToAlbum(/* pass your final url to save */) { (error) in
//Do what you want
}
您可以使用 UISaveVideoAtPathToSavePhotosAlbum
将视频保存到图库
该函数将视频添加到指定路径下的用户相册中。
func UISaveVideoAtPathToSavedPhotosAlbum(_ videoPath: String,
_ completionTarget: Any?,
_ completionSelector: Selector?,
_ contextInfo: UnsafeMutableRawPointer?)
在 VideoPath 中,插入要保存的视频的路径。
此外,您可以先使用功能UIVideoAtPathCompatibleWithSavedPhotosAlbum(_:)
检查未保存的错误,看看视频是否可以存储在图库中。
有关详细信息,请参阅 apple developer site。
提供的两个答案都很好。我选择 Hailey's 作为 'correct' 之一,因为它非常适合简单地保存视频。对于将来遇到此 post 的任何人。您可以根据需要使用其中任何一种。 Schaheer 的代码处理请求权限和保存视频,而 Hailey 的代码只是在您有路径的情况下保存视频。我将上述两个答案结合起来,并将其与其他来源提供的示例代码一起使用。
对于使用 IMGLY 照片或视频编辑器 SDK 的任何其他人,我将 post 我很快使用的代码。