iOS 9使用PHPhotoLibrary和swift 2

时间:2015-10-06 07:08:48

标签: swift filenames ios9 photo

我尝试使用带有.addResourceWithType和Swift 2的iOS 9 PHAssetCreationRequest来保存带有自定义文件名的照片。它正在保存照片,但它没有使用我在PHAssetResourceCreationOptions中指定的文件名选项。我想使用自定义文件名,因为我需要存储一个带有照片的自定义字符串,该字符串表示'键入'照片是。目前我正在存储'类型'然而,如果应用程序已卸载,那么Core Data数据存储中的localIdentifier也是如此,因此我正在寻找与照片保持一致的其他方法。 以下是我使用的代码:

var assetPlaceholder:PHObjectPlaceholder!
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
    if #available(iOS 9.0, *) {
        let options = PHAssetResourceCreationOptions()
        options.originalFilename = frameType + "_" + origFilename
        let newcreation:PHAssetCreationRequest = PHAssetCreationRequest.creationRequestForAsset()
        newcreation.addResourceWithType(PHAssetResourceType.Photo, data:UIImageJPEGRepresentation(image, 1)!, options: options)
        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

0 个答案:

没有答案