ios - 如何从库中获取所有照片

标签 ios swift photos

下面的代码是我试过的。我想实现的是从设备中获取所有照片。目前只获取了一些。如何修改代码以加载所有图像从设备?

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/

相关文章:

ios - xcode 目标中没有 iOS 应用程序

ios - for-in 循环不区分类?

ios - 照片库权限 - 为什么我的应用程序没有显示在“设置”应用程序中? (iOS 12.1 Xcode 10.1)

ios - 使用evaluateJavaScript与iOS应用程序进行WebView通信存在安全风险

html - 图片上有一个奇怪的图标和轮廓

iOS 12 - 手电筒打不开

iphone - 编写父 View Controller 的终极指南

javascript - 如何快速获取html文本输入值和输出

facebook-graph-api - 为什么 API 不返回对经过身份验证的用户可见的照片和相册? (有friends_photos 权限)

ios - 从本地 URL 获取图像 (iOS)