ios - Swift 中的照片框架升序

标签 ios swift photosframework

我的项目中运行了一个collectionView,它通过使用照片框架成功地获取iPhone照片库。我正在尝试从上一张照片提升照片collectionView。我正在使用以下代码....

  func getAllPhotosInfo() {
   photoAssets = []

    let options = PHFetchOptions()
    options.sortDescriptors = [
        NSSortDescriptor(key: "creationDate", ascending: false)
    ]

    let assets: PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: options)
    assets.enumerateObjectsUsingBlock { (asset, index, stop) -> Void in
        self.photoAssets.append(asset as! PHAsset)
    }
    print(photoAssets)
}

//PHAsset到图像转换

import UIKit
import Photos

class CollectionViewCell: UICollectionViewCell {

var imageManager: PHImageManager?

@IBOutlet weak var photoImageView: UIImageView!

var imageAsset: PHAsset? {
    didSet {

            self.imageManager?.requestImageForAsset(self.imageAsset!, targetSize: CGSize(width: 320, height: 320), contentMode: .AspectFill, options: nil) { image, info in
            self.photoImageView.image = image
        }

    }
}

}

最佳答案

我正在研究照片框架文档,我找到了答案。这是工作代码......

var photoAssets = [PHAsset]()

func getAllPhotosInfo() {
   photoAssets = []

   let options = PHFetchOptions()
   options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
   options.predicate =  NSPredicate(format: "mediaType = %i", PHAssetMediaType.Image.rawValue)
   let assetCollections = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.SmartAlbum, subtype: PHAssetCollectionSubtype.SmartAlbumUserLibrary, options: nil)

for i in 0 ..< assetCollections.count
{

        if let assetCollection = assetCollections[i] as? PHAssetCollection
        {
           images = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: options)

             print(images.count)

            return
        }
   }
}

关于ios - Swift 中的照片框架升序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37275302/

相关文章:

objective-c - 无法识别Objective-C类

ios - 如何访问此 Plist 中的数据?

ios - 如何像 ALAssetsLibrary 一样使用 PHPhotoLibrary

ios - Auto Layout iOS 7 - 无法将 subview 的右侧与 UIScrollView 的右侧对齐

ios - 如何处理用户从WKWebView上传的图像或视频?

iOS - 如何从容器 View 访问变量并将其传递回 View Controller

swift - DateComponentsFormatter 在组件字符串上崩溃

swift - 为什么 swift 编译器不接受以下映射闭包

ios8 - 如何使用 Photos API 在 iOS 8.0 中检索照片扩展名 (jpg/png)?

ios - 请求 AVAssets 时维护顺序