簡體   English   中英

照片和視頻未保存在我的自定義相冊中

[英]Photos and Videos are not saving in my Custom Photo Album

照片和視頻未保存在我的自定義相冊(TestAlbum)中。 而是將其保存在相機膠卷中。 您能否幫助我確定問題並幫助我更正代碼。

PHPhotoLibrary.shared().performChanges({
    let albumName = "TestAlbum"
    let assetCollection: PHAssetCollection!
    let fetchOptions = PHFetchOptions()
    PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)

    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)

    let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

    if let _: AnyObject = collection.firstObject {
        collection.firstObject
    }

    let options = PHAssetResourceCreationOptions()
    options.shouldMoveFile = true

    let creationRequest = PHAssetCreationRequest.forAsset()

    creationRequest.addResource(with: .video, fileURL: outputFileURL, options: options)
}, completionHandler: { success, error in
    if !success {
        print("Could not save movie to photo library: \(String(describing: error))")
    }
    cleanUp()
})

就像上面的評論一樣,我將相冊的創建和照片/視頻的保存分為兩個performChanges塊。 但是我在您的代碼中都看不到您實際上是在將資產(照片/視頻)添加到資產集合(相冊)中的位置。 為此,請執行以下操作

let assetPlaceholder = creationRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for:collection)
if let changeRequest = albumChangeRequest, let placeholder = assetPlaceholder {
    changeRequest.addAssets(NSArray(array:[placeholder]))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM