2
取AVAssetExportSession的輸出,我試圖建立一個小玩具iOS應用,從用戶的照片庫合併兩個視頻資產時。我已經得到的地步,我已經合併使用AVMutableComposition
實例的影片,現在我需要出口組成。我用下面的代碼這樣做:獲得空的結果試圖從PHAsset
func saveEditedComposition(_ composition: AVMutableComposition) {
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let savePath = (documentDirectory as NSString).strings(byAppendingPaths: ["mergeVideo.mov"])[0]
let url = NSURL(fileURLWithPath: savePath)
// Set up exporter
guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else { return }
exporter.outputURL = url as URL
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.shouldOptimizeForNetworkUse = true
// Perform the export
exporter.exportAsynchronously(completionHandler: {() -> Void in
// Upon completion of the export,
DispatchQueue.global().async {
if exporter.status == .completed {
let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [exporter.outputURL!], options: nil))
let phAsset = fetchResult.firstObject! // Crashes here, returning nil.
}
}
})
}
的問題是,在運行completionHandler時,我能看到我的出口商已沒有錯誤(if exporter.status == .completed {
)完成了,但是當我嘗試訪問該資產在exporter.outputURL
,它返回一個空的PHFetchResult<PHAsset>
。任何人都可以看到我在這裏做錯了嗎?
謝謝!您的'PHPhotoLibrary'代碼完成了這項工作。 – azrosen92