Reputation: 29
I'm trying to access the AssetCollection Via Asset. Photo framework return nil when the code get excuted. Just for additional information: Asset is picture clicked with iPhone camera and hence, it's stored under Camera Roll.
77AF0C78-242A-4979-8A7B-7CFF04014C54/L0/001 fatal error: unexpectedly found nil while unwrapping an Optional value
func assetCollectionForAsset(asset: PHAsset) -> PHCollection {
var assetCollection: PHAssetCollection!
print(asset.localIdentifier)
let fetchOptions = PHFetchOptions()
let collection = PHAssetCollection.fetchAssetCollectionsContainingAsset(asset, withType: PHAssetCollectionType.Album, options: fetchOptions)
//let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([asset.localIdentifier], options: fetchOptions)
assetCollection = collection.firstObject as! PHAssetCollection
return assetCollection
}
if I change the type to Moment, problem goes off.
let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsContainingAsset(asset, withType: PHAssetCollectionType.Moment, options: fetchOptions)
Constants information from Apple documentation says:
Album - An album in the Photos app. Albums can be created in the Photos app or appear on an iOS device through iTunes sync.
SmartAlbum - A smart album whose contents update dynamically. The Photos app displays built-in smart albums to group certain kinds of related assets (see Asset Collection Subtypes).
Moment - A moment in the Photos app. The Photos app automatically creates moments to group assets by time and location.
It means Camera Roll under Albums is not Album but a moment? I'm really confused.
Upvotes: 1
Views: 2050
Reputation: 6303
The Camera Roll does not have the collection type PHAssetCollectionType.Album
Please try with the collection type
PHAssetCollectionType.SmartAlbum
with the subtype PHAssetCollectionSubtype.SmartAlbumUserLibrary
Upvotes: 1