下面显示的代码是我已经尝试过的代码。我希望实现的是从设备中获取所有照片。目前仅获取其中的一些。如何修改代码以从设备中加载所有图像?
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
隐私权-图片库使用说明
并提供显示给用户的字符串。
索取所有图片:
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")
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句