尝试从 PHAsset 获取 AVAssetExportSession 的输出时得到空结果

Getting empty result when trying to fetch the output of AVAssetExportSession from PHAsset

我正在尝试构建一个小玩具 iOS 应用程序,它合并了用户照片库中的两个视频资产。我已经到了使用 AVMutableComposition 实例合并视频的地步,现在我需要导出合成。我正在使用以下代码这样做:

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,它 returns 一个空的 PHFetchResult<PHAsset>。谁能看出我在这里做错了什么?

您正在将视频导出到文件系统(Documents 目录),但正在从相机胶卷中获取/ALAssetsLibrary

这取决于您需要对导出的文件做什么。如果需要在ALAssetsLibrary,可以使用

ALAssetsLibrary().writeVideoAtPath(toSavedPhotosAlbum: exporter.outputURL!) { alAssetURL, error in      
}

但是 ALAssetsLibrary 现已弃用,您应该使用 PHPhotoLibrary。我不知道照片框架,但我认为它是这样工作的:

var placeHolder: PHObjectPlaceholder?

PHPhotoLibrary.shared().performChanges({
    let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: exporter.outputURL!)
    if let changeRequest = changeRequest {
        // maybe set date, location & favouriteness here?
        placeHolder = changeRequest.placeholderForCreatedAsset
    }
}) { success, error in
    placeHolder?.localIdentifier    // should identify asset from now on?
}

如果您不需要将视频保存在相机胶卷中,则可以直接使用 exporter.outputURL! 中的文件。