下面显示的代码是我已经尝试过的代码。我希望实现的是从设备中获取所有照片。目前仅获取其中的一些。如何修改代码以从设备中加载所有图像?
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")
}
}