下面的代码是我试过的。我想实现的是从设备中获取所有照片。目前只获取了一些。如何修改代码以加载所有图像从设备?
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
}
最佳答案
更新了 David 针对 iOS 10+ 和 Swift 4.x/3.x 的回答:
向设备请求访问照片的权限:
将以下值添加到您的 info.plist
Privacy - Photo Library Usage Description
并提供一个显示给用户的字符串。
请求所有图片:
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")
}
}
关于ios - 如何从库中获取所有照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38059953/