菜鸟教程小白 发表于 2022-12-11 17:06:09

ios - 检查照片库是否为空


                                            <p><p>我想使用 Swift 将照片从我的照片库加载到我的 UIImageView。现在我想检查画廊是否为空。最好的方法是什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用照片框架。</p>

<p>要查看有多少张照片,您可以执行以下操作:</p>

<pre><code>   import Photos

   ...

   PHPhotoLibrary.requestAuthorization { (status) in
       if status == PHAuthorizationStatus.Authorized {
         var result: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
         NSLog(&#34;%d&#34;, Int(result.count))
       }
   }
</code></pre>

<p>如果您想要照片和视频:</p>

<pre><code> PHPhotoLibrary.requestAuthorization { (status) in
    if status == PHAuthorizationStatus.Authorized {
       let options = PHFetchOptions()
       options.predicate = NSPredicate(format: &#34;mediaType = %i OR mediaType = %i&#34;, PHAssetMediaType.Image.rawValue, PHAssetMediaType.Video.rawValue)

       var allPhotos = PHAsset.fetchAssetsWithOptions(options)
    }
}
</code></pre>

<p>您还可以查看 PHFetchOptions 的 includeAssetSourceTypes 属性。您可以选择 TypeUserLibrary、TypeCloudShared 或 TypeiTunesSynced。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 检查照片库是否为空,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38582775/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38582775/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 检查照片库是否为空