2015-10-05 55 views
1

我寫下面的代碼保存照片在相冊中。它在iOS8中工作正常,但在iOS9中不起作用。如何使用iOS9中的PHPhotolibrary在metadat相冊中保存照片?

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

請給我一些建議來解決這個問題。

+0

你想通了嗎? – CoachNono

回答

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可用的功能做到了,但我已刪除,從這個例子。

相關問題