将PhotoLibrary中的视频和照片加载到集合视图中

时间:2017-03-23 11:30:10

标签: video swift3 photos collectionview

我正在尝试将照片库中的所有视频和照片加载到collectionView。我从stackOver flow link here获得了代码。但它在viewwillAppear函数中崩溃,在解包可选值时发现nil。我在设备和模拟器中都尝试过。我在下面添加相同的代码。

import UIKit
import Photos


class TestViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var cameraRollCollectionView: UICollectionView!

var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult<AnyObject>!
var assetThumbnailSize: CGSize!

override func viewDidLoad() {
    super.viewDidLoad()

    let fetchOptions = PHFetchOptions()

    let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

    if let first_Obj:AnyObject = collection.firstObject{
        //found the album
        self.assetCollection = first_Obj as! PHAssetCollection
    }
}

override func viewWillAppear(_ animated: Bool) {
    // Get size of the collectionView cell for thumbnail image
    if let layout = self.cameraRollCollectionView!.collectionViewLayout as? UICollectionViewFlowLayout{
        let cellSize = layout.itemSize

        self.assetThumbnailSize = CGSize(width: cellSize.width, height: cellSize.height)
    }

    //fetch the photos from collection
    self.photosAsset = (PHAsset.fetchAssets(in: self.assetCollection, options: nil) as AnyObject!) as! PHFetchResult<AnyObject>!


    self.cameraRollCollectionView!.reloadData()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: UICollectionViewDataSource

func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    var count: Int = 0

    if(self.photosAsset != nil){
        count = self.photosAsset.count
    }

    return count;
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraCell", for: indexPath as IndexPath) as! UserImagesCollectionViewCell

    //Modify the cell
    let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset

    PHImageManager.default().requestImage(for: asset, targetSize: self.assetThumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
        if result != nil {
            cell.userImage.image = result
        }
    })

    return cell
}

// MARK: - UICollectionViewDelegateFlowLayout methods
func collectionView(collectinView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 4
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 1
}

// UIImagePickerControllerDelegate Methods
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
    picker.dismiss(animated: true, completion: nil)
}

}

  

崩溃点是:

 //fetch the photos from collection
self.photosAsset = (PHAsset.fetchAssets(in: self.assetCollection, options: nil) as AnyObject!) as! PHFetchResult<AnyObject>!

请帮我解决这个问题。

谢谢

2 个答案:

答案 0 :(得分:1)

  1. 确保您拥有&#34;隐私权 - 照片库使用说明&#34;在您的&#34; Info.plist&#34;。 (见下图)此步骤将使您的应用程序可以请求照片库权限。
  2. Privacy - Photo Library Usage Description

    1. 确保您至少有一张&#34;相册&#34;在你的照片库中。或者您可以考虑将行PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, ...更改为PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, ...。 &#34; SmartAlbum&#34;可以找到&#34; CameraRoll&#34;。
    2. 祝你好运!

答案 1 :(得分:1)

请尝试.moment而不是.album。这对Swift 4来说很有用。

let collection : PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .moment, subtype: .any, options: fetchOptions)