如何从库中获取所有照片
How to fetch all photos from library
下面显示的代码是我尝试过的 already.What 我希望实现的是从 device.Currently 中获取所有照片,只有少数正在 fetched.How 修改代码以便从设备加载所有图像?
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
}
所有照片都非常简单,但您需要先确保您已获得授权。这里有一些简单的代码来演示:
PHPhotoLibrary.requestAuthorization { (status) in
switch status
{
case .Authorized:
print("Good to proceed")
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
print("Found \(allPhotos.count) images")
case .Denied, .Restricted:
print("Not allowed")
case .NotDetermined:
print("Not determined yet")
}
}
在我的 phone 这个 returns 25750 个项目上。在新的模拟器上,这应该会产生 5 张图像。
更新了大卫对 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")
}
}
下面显示的代码是我尝试过的 already.What 我希望实现的是从 device.Currently 中获取所有照片,只有少数正在 fetched.How 修改代码以便从设备加载所有图像?
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
}
所有照片都非常简单,但您需要先确保您已获得授权。这里有一些简单的代码来演示:
PHPhotoLibrary.requestAuthorization { (status) in
switch status
{
case .Authorized:
print("Good to proceed")
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
print("Found \(allPhotos.count) images")
case .Denied, .Restricted:
print("Not allowed")
case .NotDetermined:
print("Not determined yet")
}
}
在我的 phone 这个 returns 25750 个项目上。在新的模拟器上,这应该会产生 5 张图像。
更新了大卫对 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")
}
}