Last active
August 29, 2015 14:21
-
-
Save roana0229/3e3ba5c1926a40ad8486 to your computer and use it in GitHub Desktop.
ios7,8でアルバム名を指定して画像を保存する ref: http://qiita.com/roana0229/items/89f13d609116b33c8133
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 保存に成功した時 | |
PhotoAlbumUtilResult.SUCCESS | |
// 保存orアルバム生成orアルバムに追加が失敗した時 | |
PhotoAlbumUtilResult.ERROR | |
// アプリ内で写真へのアクセス認証を一度も行っていないか、認証が許可されなかった時 | |
PhotoAlbumUtilResult.DENIED |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let image = UIImage() | |
let name = "アルバム名" | |
PhotoAlbumUtil.saveImageInAlbum(image, albumName: name, completion: { (result) in | |
switch result { | |
case .SUCCESS: | |
// 保存に成功した時 | |
break | |
case .ERROR: | |
// 保存orアルバム生成orアルバムに追加が失敗した時 | |
break | |
case .DENIED: | |
// アプリ内で写真へのアクセス認証を一度も行っていないか、認証が許可されなかった時 | |
break | |
default: | |
break | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Must import AssetsLibrary.framework(Required), Photos.framework(Optional) from Targets > General > Linked Frameworks and Libraries | |
import Photos | |
import AssetsLibrary | |
enum PhotoAlbumUtilResult { | |
case SUCCESS, ERROR, DENIED | |
} | |
class PhotoAlbumUtil: NSObject { | |
class func isAuthorized() -> Bool { | |
if (UIDevice.currentDevice().systemVersion as NSString).floatValue < 8 { | |
return ALAssetsLibrary.authorizationStatus() == ALAuthorizationStatus.Authorized || ALAssetsLibrary.authorizationStatus() == ALAuthorizationStatus.NotDetermined | |
} else { | |
return PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized || PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.NotDetermined | |
} | |
} | |
class func saveImageInAlbum(image: UIImage, albumName: String, completion: ((result: PhotoAlbumUtilResult) -> ())?) { | |
if albumName.isEmpty { | |
completion?(result: .ERROR) | |
return | |
} | |
if (UIDevice.currentDevice().systemVersion as NSString).floatValue < 8 { | |
if !isAuthorized() { | |
completion?(result: .DENIED) | |
return | |
} | |
var found = false | |
let library = ALAssetsLibrary() | |
library.enumerateGroupsWithTypes(ALAssetsGroupAlbum, usingBlock: { (group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) in | |
if group != nil { | |
if albumName == group.valueForProperty(ALAssetsGroupPropertyName) as! String { | |
found = true | |
library.writeImageToSavedPhotosAlbum(image.CGImage, orientation: ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!, completionBlock: { (assetUrl: NSURL!, error: NSError!) in | |
library.assetForURL(assetUrl, resultBlock: { (asset: ALAsset!) in | |
group.addAsset(asset) | |
completion?(result: .SUCCESS) | |
}, failureBlock: { (error: NSError!) in | |
println(error.localizedDescription) | |
completion?(result: .ERROR) | |
}) | |
}) | |
} | |
} else { | |
if !found { | |
library.writeImageToSavedPhotosAlbum(image.CGImage, orientation: ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!, completionBlock: { (assetUrl: NSURL!, error: NSError!) in | |
library.addAssetsGroupAlbumWithName(albumName, resultBlock: { (group: ALAssetsGroup!) in | |
library.assetForURL(assetUrl, resultBlock: { (asset: ALAsset!) in | |
group.addAsset(asset) | |
completion?(result: .SUCCESS) | |
}, failureBlock: { (error: NSError!) in | |
println(error.localizedDescription) | |
completion?(result: .ERROR) | |
}) | |
}, failureBlock: { (error: NSError!) in | |
println(error.localizedDescription) | |
completion?(result: .ERROR) | |
}) | |
}) | |
} | |
} | |
}, failureBlock: { (error: NSError!) in | |
println(error.localizedDescription) | |
completion?(result: .ERROR) | |
}) | |
} else { | |
if !isAuthorized() { | |
completion?(result: .DENIED) | |
return | |
} | |
var assetAlbum: PHAssetCollection? | |
let list = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: nil) | |
list.enumerateObjectsUsingBlock{ (album, index, isStop) in | |
let assetCollection = album as! PHAssetCollection | |
if albumName == assetCollection.localizedTitle { | |
assetAlbum = assetCollection | |
isStop.memory = true | |
} | |
} | |
if let album = assetAlbum { | |
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ | |
let result = PHAssetChangeRequest.creationRequestForAssetFromImage(image) | |
let assetPlaceholder = result.placeholderForCreatedAsset | |
let albumChangeRequset = PHAssetCollectionChangeRequest(forAssetCollection: album) | |
albumChangeRequset.addAssets([assetPlaceholder]) | |
}, completionHandler: { (isSuccess: Bool, error: NSError!) in | |
if isSuccess { | |
completion?(result: .SUCCESS) | |
} else{ | |
println(error.localizedDescription) | |
completion?(result: .ERROR) | |
} | |
}) | |
} else { | |
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ | |
PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName) | |
}, completionHandler: { (isSuccess, error) in | |
self.saveImageInAlbum(image, albumName: albumName, completion: completion) | |
}) | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment