【问题标题】:Photos framework ascending in Swift照片框架在 Swift 中的提升
【发布时间】:2016-05-17 11:49:15
【问题描述】:

我的项目中正在运行一个 collectionView,它通过使用 Photos 框架成功获取 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
        }

    }
}

}

【问题讨论】:

  • 到目前为止一切顺利......似乎有什么问题?
  • @luk2302 我正在 ViewDidLoad 中运行 getAllPhotosInfo() 函数,但它没有提升集合视图...

标签: ios swift photosframework


【解决方案1】:

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

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
        }
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 2017-08-24
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多