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("%d", Int(result.count))
}
}
</code></pre>
<p>如果您想要照片和视频:</p>
<pre><code> 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)
}
}
</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]