2017-03-07 6 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

강제로 랩을 해제하지 마십시오 선택적 항목. 이것은 무엇입니까 가드 키워드에 대한하자. 충돌하는 행을 다음으로 바꿉니다.

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

self.videoArray.append(video)