我有一个奇怪的问题。我有一个表视图,它的单元格包含一个视频。我将AVPlayerLayer设置为拉伸到单元格的大小,但是当tableview首次加载时,它不会这样做,仅对前2个单元格。对于单元格3 +它正确加载,当我从下到上向上滚动桌面视图时,单元格确实正确加载前2个。我创建了一个13秒的YouTube视频来准确显示我的意思。注意视频前几秒内前两个单元格底部的空白区域。当我向上滚动时,该空间不存在,AVPlayerLayer的边界是正确的。
https://www.youtube.com/watch?v=q7BlZd77QVU
以下是我在cellForRowAtIndexPath中设置tableView单元格的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *mov = @".mov";
NSString *fullComponent = [NSString stringWithFormat: @"%@%@", self.videoChallengeID, mov];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:fullComponent];
NSURL *url = [NSURL fileURLWithPath:fullPath];
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, videoCell.frame.size.width, videoCell.frame.size.height)];
blueView.backgroundColor = [UIColor blueColor];
[videoCell.contentView insertSubview:blueView belowSubview:videoCell.bottomView];
videoCell.item = [AVPlayerItem playerItemWithURL:url];
self.player = [[AVPlayer alloc] initWithPlayerItem:videoCell.item];
[self.player setMuted:YES];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = CGRectMake(0, 0, videoCell.frame.size.width, videoCell.frame.size.height);
playerLayer.backgroundColor = [UIColor lightGrayColor].CGColor;
playerLayer.videoGravity = AVLayerVideoGravityResize;
[blueView.layer addSublayer:playerLayer];
[self.player play];
我清楚地正确设置了框架,除了加载的第一个2个单元格之外,它是有效的,第一次加载tableview。每隔一次它正常运行。知道会发生什么吗?
答案 0 :(得分:1)
倍率
-(void)layoutSubviews {
[super layoutSubviews]
}
在您的手机上并设置图层框
layer.frame = self.bounds
您的单元格似乎设置效率非常低,您应该重复使用视图和图层之类的内容以获得最佳性能并且只需传入您的播放器。
答案 1 :(得分:1)
因为第一次对细胞进行实例化,细胞本身的框架不会设置为它应该是什么。当它们被重复使用时,它很好但不是在它们第一次实例化时。
您应该做的是创建自定义UItableViewCell子类,然后在layoutSubviews
函数中正确设置视频图层的框架