如何从图库中获取所有照片 [英] How to fetch all photos from library
本文介绍了如何从图库中获取所有照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面显示的代码是我已经尝试过的代码.我希望实现的是从设备中获取所有照片.目前仅获取了其中一些.如何修改代码以加载所有图像来自设备?
The code shown below is what i've tried already.What I wish to achieve is to fetch all the photos from the device.Currently only a few are being fetched.How to modify the code so as to load all the images from device?
let fetchOptions = PHFetchOptions()
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Moment, subtype: .Any, options: fetchOptions)
if let first_Obj:AnyObject = collection.firstObject
{
self.assetCollection = first_Obj as! PHAssetCollection
}
推荐答案
更新了iOS 10及以上版本David的答案. Swift 4.x/3.x :
Updated David's answer for iOS 10+ & Swift 4.x/3.x:
从设备请求访问照片的权限:
将以下值添加到您的info.plist
隐私-图片库使用说明
Privacy - Photo Library Usage Description
并提供显示给用户的字符串.
And provide a string that is shown to the user.
请求所有图像:
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
print("Found \(allPhotos.count) assets")
case .denied, .restricted:
print("Not allowed")
case .notDetermined:
// Should not see this when requesting
print("Not determined yet")
}
}
这篇关于如何从图库中获取所有照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文