1

我编写了以下代码将照片保存在相册中。它在 iOS8 中工作正常,但在 iOS9 中不起作用。

PHPhotoLibrary.sharedPhotoLibrary().performChanges({

        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(newImage!)

        let assetPlaceholder = assetRequest.placeholderForCreatedAsset

        let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album, assets: albumFetch)

        albumChangeRequest!.addAssets([assetPlaceholder!])

        }, completionHandler: { success, error in

            print("added image to album")

            print(error)



    })

我得到了错误

“错误域 = NSCocoaErrorDomain”

在 iOS9 中。

请给我一些建议来解决这个问题。

4

1 回答 1

0

这是我如何实现它的未经测试的示例:

var assetPlaceholder:PHObjectPlaceholder!
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
    if #available(iOS 9.0, *) {
        let newcreation:PHAssetCreationRequest = PHAssetCreationRequest.creationRequestForAsset()    
        newcreation.addResourceWithType(PHAssetResourceType.Photo, data:UIImageJPEGRepresentation(image, 1)!, options: nil)    
        assetPlaceholder = newcreation.placeholderForCreatedAsset
    } else { // < ios 9
        // Fallback on earlier versions
        let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)    
        assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset     
    }
    let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: assetCollection)
    albumChangeRequest!.addAssets([assetPlaceholder])
}, completionHandler: {(success:Bool, error:NSError?) in
    print("added image to album")
    print(error)
})

我这样做的部分原因是我需要使用仅在 ios 9 中可用的功能,但我已从本示例中删除了该功能。

于 2016-09-05T16:09:02.583 回答