【问题标题】:How to fetch all assets from iPhone apart from camera roll?除了相机胶卷,如何从 iPhone 中获取所有资产?
【发布时间】:2018-06-19 10:01:14
【问题描述】:

如何从包括图片和视频在内的所有相册中获取资源?

let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)

此代码将仅从相机胶卷中获取图像。

【问题讨论】:

    标签: ios swift photo


    【解决方案1】:

    您可以使用此代码从 iPhone 获取所有资产。

    PHPhotoLibrary.shared().register(self as PHPhotoLibraryChangeObserver)
    
            if fetchResult == nil {
    
                let allPhotosOptions = PHFetchOptions()
                allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
                fetchResult = PHAsset.fetchAssets(with: allPhotosOptions)
            }
    

    【讨论】:

    • 这将获取所有图像和视频,但不是所有来自其他专辑的图像和视频,例如 .sync 专辑。
    【解决方案2】:

    我们可以获取所有照片,包括同步相册和用户相册。

    let fetchOptions = PHFetchOptions()
        fetchOptions.includeAssetSourceTypes = [.typeUserLibrary, .typeiTunesSynced, .typeCloudShared]
        let fetchResult = PHAsset.fetchAssets(with: fetchOptions)
    

    fetchResult 将包含所有相册中的所有照片和视频。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 2023-03-09
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多