gpt4 book ai didi

ios - 获取用于视频上传的视频 (PHAsset) 的 url - swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:25 24 4
gpt4 key购买 nike

我创建了两个不同的函数来将 PHASSet 上传到我的后端 -

uploadImageAsset() 上传所有图片 Assets :

func uploadImageAsset(asset: PHAsset, projectRef: String) {
let userID = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
let dbRef = db.collection("projects").document(projectRef)
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
option.isSynchronous = false
option.isNetworkAccessAllowed = true
option.resizeMode = .exact
option.version = .original
option.deliveryMode = .highQualityFormat
let imageSize = CGSize(width: asset.pixelWidth, height: asset.pixelHeight)
let uniqueImageID = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("project-images").child("\(uniqueImageID).jpeg")

manager.requestImage(for: asset, targetSize: imageSize, contentMode: .aspectFill, options: option) { (image, info) in
let uploadData = image?.jpegData(compressionQuality: 0.6)
storageRef.putData(uploadData!, metadata: nil, completion: {
(metadata, error) in

if error != nil {
return
} else {
storageRef.getMetadata(completion: { (metadata, err) in
if let error = err {
print(error)
} else {
storageRef.downloadURL(completion: { (url, err) in
if let error = err {
print(error)
} else {
self.imageURLs.append((url?.absoluteString)!)
dbRef.updateData(["image": self.imageURLs], completion: { (err) in
if err != nil {
print(err)
} else {
SVProgressHUD.dismiss(withDelay: 0.3)
self.dismiss(animated: true, completion: nil)
}
})
}
})
}
})
}
})
}
}

uploadProjectVideos() 上传所有视频 Assets :

func uploadVideoAsset(asset: PHAsset, projectRef: String) {
let userID = Auth.auth().currentUser?.uid
let db = Firestore.firestore()
let dbRef = db.collection("projects").document(projectRef)
let manager = PHImageManager.default()
let option = PHVideoRequestOptions()
option.isNetworkAccessAllowed = true
option.deliveryMode = PHVideoRequestOptionsDeliveryMode.highQualityFormat

manager.requestAVAsset(forVideo: asset, options: option) { (video, videoAudio, nil) in
let avurlAsset = video as! AVURLAsset
let fileURL = avurlAsset.url.absoluteString as! URL
let fileName = NSUUID().uuidString + ".mov"
let uploadTask = Storage.storage().reference().child(fileName).putFile(from: fileURL, metadata: nil, completion: { (metaData, error) in
if error != nil {
print("Failed upload of video", error)
return
}

Storage.storage().reference().child(fileName).downloadURL { (url, error) in
self.imageURLs.append("\(url)")
dbRef.updateData(["image": self.imageURLs], completion: { (err) in
if err != nil {
print(err)
} else {
SVProgressHUD.dismiss(withDelay: 0.3)
self.dismiss(animated: true, completion: nil)
}
})
}
})
}
}

在上传图片方面,我的功能非常完美。 但是,我的上传视频函数返回 nil。视频根本没有上传到后端。

这是我第一次使用 PHAssets,我真的很困惑如何我可以获取 phasset 的视频 url 并上传它

最佳答案

您可以使用文档中所示的requestAVAsset(forVideo:options:resultHandler:) 方法: https://developer.apple.com/documentation/photokit/phimagemanager/1616935-requestavasset 。这是一个最初由@iMHitesh Surani 制作的方便的扩展,由@diegomen 更新为 swift:

extension PHAsset {

func getURL(completionHandler : @escaping ((_ responseURL : URL?) -> Void)){
if self.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
completionHandler(contentEditingInput!.fullSizeImageURL as URL?)
})
} else if self.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl: URL = urlAsset.url as URL
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
}

关于ios - 获取用于视频上传的视频 (PHAsset) 的 url - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57565712/

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