我想将存储在iPhone上的视频放到我的Google云端硬盘中。 我已经完成了图像,但对于视频,这是另一个问题...
对于图像,没问题,我用这种方法将资产转换为NSData:
data = UIImagePNGRepresentation(result!)!
我把图像放到了我的驱动器上!
但是,对于视频,我尝试了许多不同的方法,但不,我不能。
我该怎么办?
非常感谢!
答案 0 :(得分:20)
我做到了!
这是解决方案:
PHCachingImageManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in
dispatch_async(dispatch_get_main_queue(), {
let asset = asset as? AVURLAsset
var data = NSData(contentsOfURL: asset.URL)
})
})
然后,你有了一个好的NSData变量,可用于将你的视频放到云端!
答案 1 :(得分:1)
已为Swift 5更新
PHImageManager
或PHCachingImageManager
可在此处使用
PHImageManager.default().requestAVAsset(forVideo: asset,
options: nil) { (asset, audioMix, info) in
if
let asset = asset as? AVURLAsset,
let data = NSData(contentsOf: asset.url) {
//do smth with data
}
}
}
答案 2 :(得分:0)
如果您错过了option.isNetworkAccessAllowed = true,那么您通常会在网址错误
private let options: PHVideoRequestOptions = PHVideoRequestOptions()
option.isNetworkAccessAllowed = true
PHImageManager.default().requestAVAsset(-------
答案 3 :(得分:0)
同步获取图像/视频 Swift 5 + 缓存
extension PHAsset {
func getImage() -> UIImage? {
let manager = PHCachingImageManager.default
let option = PHImageRequestOptions()
option.isSynchronous = true
var img: UIImage? = nil
manager().requestImage(for: self, targetSize: CGSize(width: self.pixelWidth, height: self.pixelHeight), contentMode: .aspectFit, options: nil, resultHandler: {(result, info) -> Void in
img = result!
})
return img
}
func getVideo() -> NSData? {
let manager = PHCachingImageManager.default
let option = PHImageRequestOptions()
option.isSynchronous = true
var resultData: NSData? = nil
manager().requestAVAsset(forVideo: self, options: nil) { (asset, audioMix, info) in
if let asset = asset as? AVURLAsset, let data = NSData(contentsOf: asset.url) {
resultData = data
}
}
return resultData
}
}