Swift 如何重新缩放以纵向模式拍摄的视频以适应UIView?
我有一个UICollectionView提要,用户可以在其中录制视频,视频将在提要中显示和播放。如果用户以横向模式拍摄视频,则视频非常适合UIView。但是,如果用户在纵向模式下拍摄,则在侧面有白色水平条,填充剩余部分 给定视频URL,是否有方法缩放视频以适应UICollectionView单元格中的UIView 旁白:我也想最终在线播放这些视频,这样它们就不会同时播放。有没有一个好的框架可以用来引导这个功能 UICollectionView提要中的单元格将包含一个UIView,用于存放视频播放器。这是UIView的类PlayerViewClass:Swift 如何重新缩放以纵向模式拍摄的视频以适应UIView?,swift,video,uicollectionview,scale,Swift,Video,Uicollectionview,Scale,我有一个UICollectionView提要,用户可以在其中录制视频,视频将在提要中显示和播放。如果用户以横向模式拍摄视频,则视频非常适合UIView。但是,如果用户在纵向模式下拍摄,则在侧面有白色水平条,填充剩余部分 给定视频URL,是否有方法缩放视频以适应UICollectionView单元格中的UIView 旁白:我也想最终在线播放这些视频,这样它们就不会同时播放。有没有一个好的框架可以用来引导这个功能 UICollectionView提要中的单元格将包含一个UIView,用于存放视频播放
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
}
}
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(\ucollectionview:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexath:indexPath)->CGSize{}
,以便单元格视图根据其尺寸适合视频的大小
任何帮助都将不胜感激 您可以使用
AVPlayerLayer
的videoGravity
属性。将其设置为resizeAspectFill
以便视频将填充单元格,但视频将从侧面剪切,因此请确保它适合您。非常感谢!有没有办法让视频不被切断,或者考虑到纵向和横向的尺寸,这是不可能的?也就是说,我可以更改func collectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{}
来根据视频的大小调整单元格视图的大小吗?@SergioCharles这一切都取决于您想要实现的UI。您可以使用sizeForItemAt
方法,但我不确定您是否会对结果感到满意。我可以建议您改为子类UICollectionViewLayout
class。它更灵活。谢谢!使用,UICollectionViewLayout
,我如何格式化视频单元格,以便获得与Instagram视频单元格提要类似的结果?@SergioCharles primo,Instagram使用方形视图显示视频。如果没有videoGravity
和resizeAspectFill
值,您的肖像视频将无法填充它。Secundo,你们可以试着看看互联网,我相信你们会找到好的提示,或者你们可以在这里问另一个问题,因为这是一个完全不同的问题,需要详细的解释