I want to load photos from my photo gallery to my UIImageView using Swift. Now I want to check if the gallery is empty or not. What's the best way to do that?
Asked
Active
Viewed 373 times
1 Answers
0
You could use the Photos framework.
To see how many photos there are you can do something like this:
import Photos
...
PHPhotoLibrary.requestAuthorization { (status) in
if status == PHAuthorizationStatus.Authorized {
var result: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
NSLog("%d", Int(result.count))
}
}
If you want Photos and Videos:
PHPhotoLibrary.requestAuthorization { (status) in
if status == PHAuthorizationStatus.Authorized {
let options = PHFetchOptions()
options.predicate = NSPredicate(format: "mediaType = %i OR mediaType = %i", PHAssetMediaType.Image.rawValue, PHAssetMediaType.Video.rawValue)
var allPhotos = PHAsset.fetchAssetsWithOptions(options)
}
}
You can also look to includeAssetSourceTypes property of PHFetchOptions. You can select TypeUserLibrary, TypeCloudShared or TypeiTunesSynced.

vbgd
- 1,937
- 1
- 13
- 18
-
1Do you also know how to use PHAsset for Photos and Videos combined? Now I want to check both and the fetchAssetsWithMediaType only takes one parameter e.g. .Image. – mafioso Jul 26 '16 at 07:52