我正在尝试对一个人的视频库进行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 :(得分:2)
不要强力打开选项。这就是让和守护关键字的用途。用以下内容替换崩溃线:
guard let video = video, error == nil else {
print("something bad happened")
return
}
self.videoArray.append(video)