我将如何显示多个视频的图像?当我上传多个图像和视频时

时间:2019-05-15 10:18:57

标签: ios swift uicollectionview

我正在尝试使用UICollectionView来显示多个图像,例如我的屏幕截图。有人可以建议我如何管理此设计。

Collection View Example Image

2 个答案:

答案 0 :(得分:0)

如果要在同一视图中播放多个视频,可以使用AVFoundation的AVPlayer,我已经在StackOverflow,AppleDoucument和其他论坛上阅读了有关此内容的更多信息。他们都说使用MPMoviePlayerViewController是不可能的,但是可以使用AVPlayer。

import UIKit
import AVFoundation
import MobileCoreServices
import AssetsLibrary
import MediaPlayer
import CoreMedia

class ViewController: UIViewController,UIGestureRecognizerDelegate {
var optionalInteger: Int?
var Asset1: AVAsset?
var Asset2: AVAsset?
var Asset3: AVAsset?
var audioAsset: AVAsset?
var loadingAssetOne = false


@IBOutlet weak var musicImg: UIImageView!

@IBOutlet var videoView: UIView!
@IBOutlet var videoViewTwo: UIView!

var player : AVPlayer? = nil
var playerLayer : AVPlayerLayer? = nil
var asset : AVAsset? = nil
var playerItem: AVPlayerItem? = nil

override func viewDidLoad() 
{
    super.viewDidLoad()
    musicImg.hidden = true
    let gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGesture")

    gestureRecognizer.delegate = self;
    videoPlayer.view.addGestureRecognizer(gestureRecognizer)
    imageViewVideoOne.hidden = true
    imageViewVideoTwo.hidden = true
}
func startMediaBrowserFromViewController(viewController: UIViewController!, usingDelegate delegate : protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate>!) -> Bool 
{

    if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) == false {
        return false
    }

    let mediaUI = UIImagePickerController()
    mediaUI.sourceType = .SavedPhotosAlbum



    mediaUI.mediaTypes = [kUTTypeMovie as String]
    mediaUI.allowsEditing = true
    mediaUI.delegate = delegate
    presentViewController(mediaUI, animated: true, completion: nil)
    return true

}
 // click first video
@IBAction func FirstVideo(sender: AnyObject) {
        loadingAssetOne = true
         optionalInteger = 0
        startMediaBrowserFromViewController(self, usingDelegate: self)

}

// clcik second video
@IBAction func SecondVideo(sender: AnyObject) {

        loadingAssetOne = false
        optionalInteger = 1
        startMediaBrowserFromViewController(self, usingDelegate: self)

}
// click audio
@IBAction func Audio(sender: AnyObject) {

    let mediaPickerController = MPMediaPickerController(mediaTypes: .Any)
    mediaPickerController.delegate = self
    mediaPickerController.prompt = "Select Audio"
    presentViewController(mediaPickerController, animated: true, completion: nil)
}
 @IBAction func playPreview(sender: AnyObject) 
 {
    startMediaBrowserFromViewController(self, usingDelegate: self)
 }
 extension ViewController: UIImagePickerControllerDelegate 
 {

    // display the first & second video after it picked from gallery
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    {
      let mediaType = info[UIImagePickerControllerMediaType] as! NSString
      dismissViewControllerAnimated(true, completion: nil)

      if mediaType == kUTTypeMovie 
      {
        if loadingAssetOne 
        {
          let avAsset = AVAsset(URL: info[UIImagePickerControllerMediaURL] as! NSURL)
          print(avAsset)
            if let vURL = info[UIImagePickerControllerMediaURL] as? NSURL {
                self.videoURL = vURL

            } else {
                print("oops, no url")
            }
            mediaUI.dismissViewControllerAnimated(true, completion: nil)
  let videoURLWithPath = info[UIImagePickerControllerMediaURL] as? NSURL
            self.videoURL = videoURLWithPath!
            asset = AVAsset(URL: videoURL) as AVAsset

            playerItem = AVPlayerItem(asset: asset!)
            player = AVPlayer (playerItem: self.playerItem!)
            playerLayer = AVPlayerLayer(player: self.player)
            videoView.frame = CGRectMake(38, 57, 220, 106)
            playerLayer?.frame = videoView.frame
            videoView.layer.addSublayer(self.playerLayer!)

            player!.play()
        } 
        else 
        {
           let avAssetTwo = AVAsset(URL: info[UIImagePickerControllerMediaURL] as! NSURL)
            print(avAssetTwo)
           if let vURL = info[UIImagePickerControllerMediaURL] as? NSURL {
                self.videoURL = vURL
            } else {
                print("oops, no url")
            }
            mediaUI.dismissViewControllerAnimated(true, completion: nil)
          let videoURLWithPath = info[UIImagePickerControllerMediaURL] as? NSURL
           self.videoURL = videoURLWithPath!
           asset = AVAsset(URL: videoURL) as AVAsset

            playerItem = AVPlayerItem(asset: asset!)
            player = AVPlayer (playerItem: self.playerItem!)
            playerLayer = AVPlayerLayer(player: self.player)
            videoView.frame =  CGRectMake(38, 206, 220, 106)
            playerLayer?.frame =  videoView.frame
            videoView.layer.addSublayer(self.playerLayer!)
            player!.play()
           }
         }
       }
 }

问题来源

Multiple Video same screen

Multiple Video Playback on iOS

Apple MPMoviePlayerViewController Document

Playing Multiple Video Simultaneously

Apple AVPlayer Document

Playing Multiple Videos

Playback

Playing Video in iOS

答案 1 :(得分:0)

您可以使用SizeForItemAt函数为每个单元格设置自定义尺寸,并在最后一个单元格内添加一个按钮以打开最后两个图像,或者使用didSelectItemAt打开最后一个单元格。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
      var collectionViewSize = collectionView.frame.size
   if indexPath.row == 0 {
      collectionViewSize.width = collectionViewSize.width/2.0 //Display 2 items in a row.
      collectionViewSize.height = collectionViewSize.height/2.0
     }
 if indexPath.row == 1 {

  collectionViewSize.width = collectionViewSize.width/3.0 //Display 3 items in row
  collectionViewSize.height = collectionViewSize.height/2.0
  }
      return collectionViewSize
}





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



   func collectionView(_ collectionView: UICollectionView, cellForItemAt    indexPath:  IndexPath) -> UICollectionViewCell {
 let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "YourCustomCell", for: indexPath) as! YourCustomCell 
cell.imageView.image = yourImageArray[indexPath.item] as UIImage
return cell 
}

*请记住在ViewController中设置UICollectionViewDelegateFlowLayout