如何重新缩放以纵向模式拍摄的视频以适合 UIView?
How do I re-scale a video taken in portrait mode to fit a UIView?
我有一个 UICollectionView feed 用户可以在其中录制视频,视频将在 feed 中显示和播放。如果用户以横向模式拍摄视频,则该视频非常适合 UIView。但是,如果用户以纵向模式拍摄,则两侧会出现白色水平条,填充多余部分。
Given the video URL, is there a way to scale the video to fit the UIView in a cell of the UICollectionView?
旁白:我还想最终在线播放这些视频,这样它们就不会同时播放。我可以使用一个好的框架来实现 bootstrap 这个功能吗?
UICollectionView 提要中的一个单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的 class PlayerViewClass:
import Foundation
import UIKit
import AVKit
import AVFoundation
class PlayerViewClass: UIView {
override static var layerClass: AnyClass {
return AVPlayerLayer.self
}
var playerLayer: AVPlayerLayer {
return layer as! AVPlayerLayer
}
var player: AVPlayer? {
get {
return playerLayer.player
}
set {
playerLayer.player = newValue
}
}
}
请注意,单元格 MyCollectionViewCell 有一个链接到此 UIView 的 IBOutlet:
class MyCollectionViewCell {
@IBOutlet weak var playerView: PlayerViewClass!
override func awakeFromNib() {
super.awakeFromNib()
//Setup, not relevant
}
}
Feed在FeedViewController中的collectionView cellForItemAt indexPath委托方法如下:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if let cell = cell as? MyCollectionViewCell {
...
//Configuring the cell
...
//Video player
let avPlayer = AVPlayer(url: post.fullURL)
**//TODO: Scale the video to the playerView (UIView)**
//Setting cell's player
cell.playerView.playerLayer.player = avPlayer
//Play
cell.playerView.player?.play()
}
return cell
}
或者,我可以设置 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { }
以便单元格视图根据其尺寸适合视频的大小吗?
如有任何帮助,我们将不胜感激!
您可以使用 videoGravity
属性 个 AVPlayerLayer
。将其设置为 resizeAspectFill
,这样视频将填满单元格,但视频的两边会被剪切,因此请确保它适合您。
我有一个 UICollectionView feed 用户可以在其中录制视频,视频将在 feed 中显示和播放。如果用户以横向模式拍摄视频,则该视频非常适合 UIView。但是,如果用户以纵向模式拍摄,则两侧会出现白色水平条,填充多余部分。
Given the video URL, is there a way to scale the video to fit the UIView in a cell of the UICollectionView?
旁白:我还想最终在线播放这些视频,这样它们就不会同时播放。我可以使用一个好的框架来实现 bootstrap 这个功能吗?
UICollectionView 提要中的一个单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的 class PlayerViewClass:
import Foundation
import UIKit
import AVKit
import AVFoundation
class PlayerViewClass: UIView {
override static var layerClass: AnyClass {
return AVPlayerLayer.self
}
var playerLayer: AVPlayerLayer {
return layer as! AVPlayerLayer
}
var player: AVPlayer? {
get {
return playerLayer.player
}
set {
playerLayer.player = newValue
}
}
}
请注意,单元格 MyCollectionViewCell 有一个链接到此 UIView 的 IBOutlet:
class MyCollectionViewCell {
@IBOutlet weak var playerView: PlayerViewClass!
override func awakeFromNib() {
super.awakeFromNib()
//Setup, not relevant
}
}
Feed在FeedViewController中的collectionView cellForItemAt indexPath委托方法如下:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if let cell = cell as? MyCollectionViewCell {
...
//Configuring the cell
...
//Video player
let avPlayer = AVPlayer(url: post.fullURL)
**//TODO: Scale the video to the playerView (UIView)**
//Setting cell's player
cell.playerView.playerLayer.player = avPlayer
//Play
cell.playerView.player?.play()
}
return cell
}
或者,我可以设置 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { }
以便单元格视图根据其尺寸适合视频的大小吗?
如有任何帮助,我们将不胜感激!
您可以使用 videoGravity
属性 个 AVPlayerLayer
。将其设置为 resizeAspectFill
,这样视频将填满单元格,但视频的两边会被剪切,因此请确保它适合您。