I am using PHPhotoLibrary to create a custom folder and add pictures in it. It works ok, but the picture I am adding to the custom folder is also added to the "camera roll" folder. I found this old thread Add photos in custom album also added in default Album "Camera Roll", where somebody says that it is the normal behaviour, but since the comment is from 3 years ago, I would like to know if in the latests iOS versions there is a way to add the picture only to the custom folder. I have searched for some documentation to confirm this, but no luck.
The code I am using:
class TMPhotoAlbum {
static let albumName = "Custom Album"
static let sharedInstance = TMPhotoAlbum()
var assetCollection: PHAssetCollection!
init() {
func fetchAssetCollectionForAlbum() -> PHAssetCollection! {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", TMPhotoAlbum.albumName)
let collection = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
if (collection.firstObject != nil) {
return collection.firstObject as! PHAssetCollection
}
return nil
}
if let assetCollection = fetchAssetCollectionForAlbum() {
self.assetCollection = assetCollection
return
}
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(TMPhotoAlbum.albumName)
}) { success, _ in
if success {
self.assetCollection = fetchAssetCollectionForAlbum()
}
}
}
func saveImage(image: UIImage, completion: (Bool, NSError?) -> Void) {
if assetCollection == nil {
return // If there was an error upstream, skip the save.
}
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceholder = assetChangeRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
albumChangeRequest?.addAssets([assetPlaceholder!])
}, completionHandler: completion )
}
}
It's still normal and expected behaviour: Photos/Videos in user created albums are only references to the original in the "Camera Roll". This concept is existent since iOS 5 and hasn't changed so far.
The only photos/videos being not part of the Camera Roll are photos/videos synced by iTunes, Shared Photostreams and items imported with the Camera Connection Kit.