我已将收藏视图中的所有本地视频显示为图像。
但现在当我点击单元格时它应该播放。
我在集合视图单元格中显示的代码如下所示
导入UIKit
导入照片
导入AVFoundation
导入AVKit
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?#>)
}
}
答案 0 :(得分: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()
})
}