【问题标题】:How to change order of PHAssetCollection list如何更改 PHAssetCollection 列表的顺序
【发布时间】:2015-11-02 13:10:59
【问题描述】:

我正在使用照片框架使用此代码在 iOS8 中获取相册列表,
如何重新排序 smartAlbums,所以我可以显示“最近添加”

 let smartAlbums : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.SmartAlbum, subtype: PHAssetCollectionSubtype.AlbumRegular, options: nil)

在 cellForRow 方法中

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let collection = smartAlbums[indexPath.row]
            cell.textLabel?.text = collection.localizedTitle
            return cell

    }

let photofetchOpt:PHFetchOptions? = PHFetchOptions()
    photofetchOpt?.sortDescriptors = [NSSortDescriptor(key:"localizedTitle", ascending: false)]

我在获取 AssetCollections 时尝试使用 PHFetchOptions,但对 smartAlbums 的顺序没有影响。

【问题讨论】:

    标签: iphone swift ios8 swift2 xcode7


    【解决方案1】:

    PHAssetCollection 类有一个属性名称startDate,表示资产集合中所有资产的最早创建日期。 Link.

    PHFetchOptions 中使用sortDescriptor 获取相册购买最新图片订单。

    Objective-C

    PHFetchOptions *options = [PHFetchOptions new];
    options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:NO]];
    PHFetchResult<PHAssetCollection*> *allCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
    

    斯威夫特

    let options = PHFetchOptions()
    options.sortDescriptors = [NSSortDescriptor(key: "startDate", ascending: false)]
    let result = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: options)
    

    【讨论】:

      【解决方案2】:

      我有一个重新排序 PHCollectionList 的解决方案,我正在为那些正在努力重新排序列表的人发布这个答案 基本上我正在使用NSMutableArray sortedSmartAlbumsCollectionsFetchResults

      if(smartAlbums.count > 0) {
          for i in 0...smartAlbums.count-1 {
              let assetCollection:PHAssetCollection = smartAlbums[i] as! PHAssetCollection
      
              if assetCollection.assetCollectionSubtype == PHAssetCollectionSubtype.SmartAlbumRecentlyAdded {
                  sortedSmartAlbumsCollectionsFetchResults.insertObject(assetCollection, atIndex: 0)
              }
              else {
                  sortedSmartAlbumsCollectionsFetchResults.addObject(assetCollection)
              }
          }
      }
      

      如上面的代码所示,我已经从 smartAlbums 获取每个 PHAssetCollection 并检查它的子类型是否为 SmartAlbumRecentlyAdded 如果是,则将其插入到 0 索引处,否则继续添加 NSMutableArray

      查看结果

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 1970-01-01
        • 2022-12-04
        • 2011-07-24
        • 2011-04-15
        • 2021-01-26
        相关资源
        最近更新 更多