如何在iOS9中使用PHPhotolibrary使用metadat在相册中保存照片?

时间:2015-10-05 07:12:48

标签: ios iphone ios9 phphotolibrary

我已编写以下代码以保存相册中的照片。它在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)



    })

我收到了错误

  

“Error Domain = NSCocoaErrorDomain”

在iOS9中

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

1 个答案:

答案 0 :(得分: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中提供的功能,但我已从此示例中删除了该功能。