2017-03-23 71 views
1

我試圖將所有視頻和照片庫中的照片加載到collectionView。我從stackOver flow link here得到了代碼。但是它在viewwillAppear函數中發生崩潰,表示在展開可選值時發現爲零。我在設備和模擬器中都試過。我在下面添加相同的代碼。將PhotoLibrary中的視頻和照片加載到收藏視圖

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

請請幫我與此有關。

謝謝

+1

的Xcode應該告訴您哪條線「中發現零而展開可選值」,提供這些信息可以幫助我們找到您的問題viewWillAppear中FUNC內 – xhg

+0

... //從集合中獲取照片 self.photosAsset =(PHAsset.fetchAssets(in:self.assetCollection,options:nil)as AnyObject!)as! PHFetchResult ! 在這一點 –

+0

任何人請幫助我@aahung –

回答

1
  1. 請確保您有「隱私 - 圖片庫使用情況說明」,在你的「的Info.plist」。 (請參閱下圖)此步驟將使您的應用程序能夠請求圖片庫許可。

Privacy - Photo Library Usage Description

  • 確保你在照片庫中至少有一個 「相冊」。或者您可以考慮將行PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, ...更改爲PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, ...。 「SmartAlbum」可以找到「CameraRoll」。
  • 祝你好運!

    +0

    謝謝你的回覆。但我的糟糕時間 –

    0

    請嘗試.moment而不是.album。這是對我工作的斯威夫特4

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