Ios 检查照片库是否为空

Ios 检查照片库是否为空,ios,swift,camera,gallery,is-empty,Ios,Swift,Camera,Gallery,Is Empty,我想使用Swift将照片从照片库加载到UIImageView。现在我想检查一下图库是否是空的。最好的方法是什么?您可以使用照片框架 要查看有多少张照片,可以执行以下操作: import Photos ... PHPhotoLibrary.requestAuthorization { (status) in if status == PHAuthorizationStatus.Authorized { var result: PHFetch

我想使用Swift将照片从照片库加载到UIImageView。现在我想检查一下图库是否是空的。最好的方法是什么?

您可以使用照片框架

要查看有多少张照片,可以执行以下操作:

   import Photos

   ...

   PHPhotoLibrary.requestAuthorization { (status) in
       if status == PHAuthorizationStatus.Authorized {
           var result: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
           NSLog("%d", Int(result.count))
       }
   }
如果您想要照片和视频:

 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)
    }
 }

您还可以查看PHFetchOptions的includeAssetSourceTypes属性。您可以选择TypeUserLibrary、TypeCloudShared或TypeiTunesynced。

您还知道如何将Phaset用于照片和视频的组合吗?现在我想检查这两个参数,fetchAssetsWithMediaType只接受一个参数,例如..Image。