2017-03-07 4 views
-3

人のビデオライブラリのUICollectionを実行しようとしています。そして、このラインでクラッシュしたアプリ:ここビデオライブラリのUICollection

self.videoArray.append(video!) 

をし、エラー自体は「悪いのアクセス」であるが、完全なコードです:

import UIKit 
import Photos 

class uploadVideoVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    var imageArray = [UIImage]() 

    var videoArray = [AVPlayerItem]() 

    func grabPhotos() { 
     let imgManger = PHImageManager.default() 

     let requestOptions = PHImageRequestOptions() 

     requestOptions.isSynchronous = true 
     requestOptions.deliveryMode = .highQualityFormat 


     let videoRequests = PHVideoRequestOptions() 
     videoRequests.deliveryMode = .highQualityFormat 

     let fetchOptions = PHFetchOptions() 
     fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] //can add multiple sorts by a comma 

     if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions) { 

      if fetchResult.count > 0 { 

       for i in 0..<fetchResult.count { 

        /** imgManger.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height:200), contentMode: .aspectFill, options: requestOptions, resultHandler: { 

         image, error in 

         self.imageArray.append(image!) 

        })**/ 

        imgManger.requestPlayerItem(forVideo: fetchResult.object(at: i), options: videoRequests, resultHandler: { video, error in 
        self.videoArray.append(video!) 
        }) 
       } 

      } else { 
       print("no photos mang") 

       //i still think we need to reload the data? 

      } 

     } 
    } 


    let topBar = UIView() 
    let videoCollectionView = UIView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     grabPhotos() 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 
     self.view.backgroundColor = UIColor.red 
     let flowLayout = UICollectionViewFlowLayout() 

     let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout) 
     collectionView.register(VideoSelectionCVCell.self, forCellWithReuseIdentifier: cellId) 

     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.backgroundColor = UIColor.cyan 

     self.view.addSubview(collectionView)  // Do any additional setup after loading the view. 
     self.navigationController?.isNavigationBarHidden = true; 

     topBar.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height/15)) 
     topBar.backgroundColor = UIColor.blue.withAlphaComponent(0) 

     // topBar.layer.cornerRadius = self.view.frame.width*0.04 

     self.view.addSubview(topBar) 

     videoCollectionView.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height/15, width:self.view.frame.size.width, height: self.view.frame.size.height)) 
     videoCollectionView.backgroundColor = UIColor.red 

     self.view.addSubview(videoCollectionView) 

     videoCollectionView.addSubview(collectionView) 


    } 



    var numOfCol : Int = 2 


    var cellId = "Cell" 

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



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

     // cell.uploadedFile.image = imageArray[indexPath.row] 

     //cell.uploadedFile.image = videoArray[indexPath.row] as! UIImage 
     cell.backgroundColor = UIColor.purple 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     let width = (collectionView.bounds.width)/2 
     let height = (collectionView.bounds.height)/4 // = 25% of the screen 



     return CGSize(width:width, height:height) 
    } 

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



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

     return 0.0 
    } 

    override var prefersStatusBarHidden: Bool { 
     get { 
      return true 
     } 
    } 

} 
+0

'video'はおそらく' nil'です。あなたは強制的にアンラッピングするのではなく、そのことを確認する必要があります。 – rmaddy

+1

目次 - エラーに関する質問を投稿するときは、質問に完全なエラーメッセージを含めるようにしてください。 – rmaddy

+0

実際にはエラーはありません。 「Bad_Access」と表示されます。 –

答えて

2

は強制的にoptionalsのラップを解除しないでください。これは、のガードを受け入れるようにしたものです。キーワードは次のとおりです。クラッシュする行を次のように置き換えてください:

guard let video = video, error == nil else { 
    print("something bad happened") 
    return 
} 

self.videoArray.append(video) 
関連する問題