视频库的UICollection

Posted

技术标签:

【中文标题】视频库的UICollection【英文标题】:UICollection of video library 【发布时间】:2017-03-07 21:06:48 【问题描述】:

我正在尝试对一个人的视频库进行 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
        
    


【问题讨论】:

video 可能是nil。您必须检查它而不是强制展开。 顺便说一句 - 当您发布有关错误的问题时,请确保在您的问题中包含完整的错误消息。 确实没有错误;它只是说“Bad_Access” 在您发布问题之前,请先进行一些基本的故障排除。完成后,请阅读How to Ask。然后发布故障排除的结果,并尝试只发布必要的代码。提示:被注释掉的代码是不必要的。 【参考方案1】:

不要强行打开可选选项。这就是 letguard let 关键字的用途。将您的崩溃行替换为:

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


self.videoArray.append(video)

【讨论】:

以上是关于视频库的UICollection的主要内容,如果未能解决你的问题,请参考以下文章

面部识别/检测 PHP 或用于照片和视频库的软件

fancybox 没有用于视频库的导航按钮,但它在小提琴中工作

Windows & Esp32基于 libjpeg-9e 编解码库的视频播放器

c#的一个视频播放库的封装-VLC.DotNet(二)

音视频交叉编译动态库静态库的学习

FreeSWITCH第三方库(视频)的简单介绍