【问题标题】:how to save photo in album with metadat using PHPhotolibrary in iOS9?如何在 iOS9 中使用 PHPhotolibrary 将照片保存在带有元数据的相册中?
【发布时间】:2015-10-05 07:12:47
【问题描述】:

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

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

【问题讨论】:

  • 你找到办法了吗?

标签: ios iphone ios9 phphotolibrary


【解决方案1】:

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

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 中可用的功能,但我已从本示例中删除了该功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多