2017-04-04 14 views
0

すべてのローカルビデオをコレクションビューでイメージとして表示しました。 しかし、私はそれを再生する必要がありますセルをクリックします。 コレクションビューのセルに表示するための私のコードは、 enter image description here 輸入のUIKit 輸入写真 輸入AVFoundation 輸入AVKit迅速にビデオを再生する方法3 Xcode 8.2.1

class VideoViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    var imageArrray = [UIImage]() 
    // var playerController = AVPlayerViewController() 
    // var player:AVPlayer? 
    // var tempVideoFetchresult = PHFetchResult<AnyObject>() 


    override func viewDidLoad() { 
     //super.viewDidLoad() 
     grabVideos() 

     // Do any additional setup after loading the view. 
    } 

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




    func grabVideos() 
    { 
     let imgManager = PHImageManager.default() 

     let requestOtions = PHImageRequestOptions() 
     requestOtions.isSynchronous = true 
     requestOtions.deliveryMode = .highQualityFormat 
     let fetchOptions = PHFetchOptions() 
     fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 

     if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions) 
     { 
     // tempVideoFetchresult = fetchResult as! PHFetchResult<AnyObject> 
      if fetchResult.count > 0 
      { 
       for i in 0..<fetchResult.count{ 
        imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width :200, height : 200), contentMode: .aspectFill, options: requestOtions, resultHandler: { 

         image, error in 

         self.imageArrray.append(image!) 

        }) 



       } 
      }else{ 
       print("You got no photos") 
       self.collectionView?.reloadData() 
      } 
     } 



    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return imageArrray.count 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! videoCollectionViewCell 

     cell.videoImageView.image = imageArrray[indexPath.row] 

     //let imageView = cell.viewWithTag(1) as! UIImageView 


     // let imageView = cell.contentView.viewWithTag(1) as! UIImageView 
     // imageView.image = imageArrray[indexPath.row] 

     return cell 
    } 


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let width = collectionView.frame.width/3 - 1 
     return CGSize(width: width, height: width) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 1.0 
    } 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
     return 1.0 
    } 

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     //play video 
       // self.player = AVPlayer(playerItem: <#T##AVPlayerItem?#>) 
    } 


} 

答えて

1

私は答えまし以下の通りである

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    let imgManager = PHImageManager.default() 

    let requestOtions = PHVideoRequestOptions() 

    requestOtions.deliveryMode = .highQualityFormat 
    imgManager.requestPlayerItem(forVideo: tempVideoFetchresult.object(at: indexPath.row) as! PHAsset, options: requestOtions, resultHandler: { 
     avplayeritem, error in 
     self.player = AVPlayer(playerItem: avplayeritem) 
     self.playerController.player = self.player 

    }) 
      // self.player = AVPlayer(playerItem: <#T##AVPlayerItem?#>) 
    self.present(self.playerController,animated: true,completion: { 
     self.playerController.player?.play() 
    }) 
} 
関連する問題