şöyle bir şey iş görür basitçe,
denedim çalışıyor.
önce bir versiyon chceker'la ios sürümünü kontrol edin.
eğer ios8 ve üzeriyse addImage methodu içerisinde önce PHPhotoLibrary'i kullanarak bir albüm oluşturun.
Photos framework'ünü import etmeyi unutmayın, eğer ios7'de PHPhotoLibrary yüzünden çökme yaşarsanız, targets / Build phases / Link Binary With Libraries listesinde eklediğiniz photos framework'unu optional yapın.
import Photos
private func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}
//method to add Image to Particular Album
func addImage(image:UIImage, metadata:NSDictionary, toAlbum albumName:String, handler:CompletionHandler){
if SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO("8.0.0") {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
var createAlbumRequest:PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
}, completionHandler: { success, error in
if success {
println("album created for ios8")
}
})
}
library?.addAssetsGroupAlbumWithName(albumName, resultBlock: {(group:ALAssetsGroup!) -> Void in
println("Album Created:= \(albumName)")
/*-- Find Group --*/
var groupToAddTo:ALAssetsGroup?
self.library?.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum),
usingBlock: { (group:ALAssetsGroup?, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
if(group != nil){
if group!.valueForProperty(ALAssetsGroupPropertyName) as! String == albumName{
groupToAddTo = group
println("Group Found \(group!.valueForProperty(ALAssetsGroupPropertyName))")
self.library?.writeImageToSavedPhotosAlbum(image.CGImage, metadata:metadata as [NSObject : AnyObject], completionBlock: {(assetURL:NSURL!,error:NSError!) -> Void in
if(error == nil){
self.library?.assetForURL(assetURL,
resultBlock: { (asset:ALAsset!) -> Void in
var yes:Bool? = groupToAddTo?.addAsset(asset)
if (yes == true){
handler(success: true)
}
},
failureBlock: { (error2:NSError!) -> Void in
println("Failed to add asset")
handler(success: false)
});
}
});
}
} /*Group Is Not nil*/
},
failureBlock: { (error:NSError!) -> Void in
println("Failed to find group")
handler(success: false)
});
}, failureBlock: { (error:NSError!) -> Void in
println("Failed to create \(error)")
handler(success: false)
});
}