AVPlayerLayer 未绑定到 UIView 框架
AVPlayerLayer not bounds to UIView frame
我正在尝试将 AVPlayerLayer 添加到 UIView
self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame = ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
player.play();
这是视频的容器(蓝色):
约束条件:
最终结果:
我不明白为什么视频不绑定到 UIVIew 坐标。如果我将它绑定到控制器的超级视图中,就可以了。
我在 viewDidLoad 事件中添加 AVPlayerLayer...在应用约束之前。
当视图的框架改变时,子图层不会自动调整大小,您必须手动调整。您可以子 class 一个 UIView 以使其更容易。
class VideoContainerView: UIView {
var playerLayer: CALayer?
override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
playerLayer?.frame = self.bounds
}
}
然后在您的代码中:
self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame = ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
self.ctrVideo.playerLayer = playerLayer;
player.play();
只需确保将 "ctrVideo" 的 class 从 UIView 更改为 VideoContainerView。
这是 AVPlayerLayer
支持的视图,试图保持其纵横比。
PlayerView.h:
@interface PlayerView : UIView
@property (strong, nonatomic) AVPlayerLayer *layer;
@end
PlayerView.m:
@interface PlayerView ()
@property (strong, nonatomic) NSLayoutConstraint *aspectConstraint;
@end
@implementation PlayerView
@dynamic layer;
+ (Class)layerClass {
return [AVPlayerLayer class];
}
- (instancetype)init {
self = [super init];
if (self) {
self.userInteractionEnabled = NO;
[self addObserver:self forKeyPath:@"layer.videoRect" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
}
return self;
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"layer.videoRect"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"layer.videoRect"]) {
CGSize size = [change[NSKeyValueChangeNewKey] CGRectValue].size;
if (change[NSKeyValueChangeNewKey] && size.width && size.height) {
self.aspectConstraint.active = NO;
self.aspectConstraint = [self.widthAnchor constraintEqualToAnchor:self.heightAnchor multiplier:size.width / size.height];
self.aspectConstraint.priority = UILayoutPriorityDefaultHigh;
self.aspectConstraint.active = YES;
}
else {
self.aspectConstraint.active = NO;
}
}
}
@end
这是@almas 在 Swift 4
中的精彩回答
class VideoContainerView: UIView {
var playerLayer: CALayer?
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
playerLayer?.frame = self.bounds
}
}
我正在尝试将 AVPlayerLayer 添加到 UIView
self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame = ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
player.play();
这是视频的容器(蓝色):
约束条件:
最终结果:
我不明白为什么视频不绑定到 UIVIew 坐标。如果我将它绑定到控制器的超级视图中,就可以了。
我在 viewDidLoad 事件中添加 AVPlayerLayer...在应用约束之前。
当视图的框架改变时,子图层不会自动调整大小,您必须手动调整。您可以子 class 一个 UIView 以使其更容易。
class VideoContainerView: UIView {
var playerLayer: CALayer?
override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
playerLayer?.frame = self.bounds
}
}
然后在您的代码中:
self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame = ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
self.ctrVideo.playerLayer = playerLayer;
player.play();
只需确保将 "ctrVideo" 的 class 从 UIView 更改为 VideoContainerView。
这是 AVPlayerLayer
支持的视图,试图保持其纵横比。
PlayerView.h:
@interface PlayerView : UIView
@property (strong, nonatomic) AVPlayerLayer *layer;
@end
PlayerView.m:
@interface PlayerView ()
@property (strong, nonatomic) NSLayoutConstraint *aspectConstraint;
@end
@implementation PlayerView
@dynamic layer;
+ (Class)layerClass {
return [AVPlayerLayer class];
}
- (instancetype)init {
self = [super init];
if (self) {
self.userInteractionEnabled = NO;
[self addObserver:self forKeyPath:@"layer.videoRect" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
}
return self;
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"layer.videoRect"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"layer.videoRect"]) {
CGSize size = [change[NSKeyValueChangeNewKey] CGRectValue].size;
if (change[NSKeyValueChangeNewKey] && size.width && size.height) {
self.aspectConstraint.active = NO;
self.aspectConstraint = [self.widthAnchor constraintEqualToAnchor:self.heightAnchor multiplier:size.width / size.height];
self.aspectConstraint.priority = UILayoutPriorityDefaultHigh;
self.aspectConstraint.active = YES;
}
else {
self.aspectConstraint.active = NO;
}
}
}
@end
这是@almas 在 Swift 4
中的精彩回答class VideoContainerView: UIView {
var playerLayer: CALayer?
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
playerLayer?.frame = self.bounds
}
}