裁剪UIView

时间:2016-01-11 10:51:02

标签: ios uiview crop

我有一个需要裁剪的视图。我有4个视图显示在主视图上查看的视频子。由于视频比例,我需要裁剪视图,使视频正方形而不是矩形。这是我的代码:

- (void)videoSize {
    CGFloat size;
    if ([self.videosView frame].size.height <= [self.emplacementView frame].size.width) {
        size = [self.emplacementView frame].size.height;
    } else {
        size = [self.emplacementView frame].size.width;
    }
    CGFloat offsetX = 0;
    CGFloat offsetY = 0;

    NSArray* keys = [mediaStreams allKeys];
    int count = keys.count;
    if ( !count ) return;

    for (int i=0; i<count; i++) {
        NSString* id = keys[i];
        MediaStream* ms = [ mediaStreams valueForKey:id ];
        switch (i) {
            case 0:
                offsetX = 0;
                offsetY = 0;
                break;
            case 1:
                offsetX = size / 2;
                offsetY = 0;
                break;
            case 2:
                offsetX = 0;
                offsetY = size / 2;
                break;
            case 3:
                offsetX = size / 2;
                offsetY = size / 2;
                break;
            default:
                break;
        }

        CGRect frame = CGRectMake(offsetX, offsetY, size / 2, size / 2);
        [ms getVideoView].getView.frame = frame;
        [ms getVideoView].getView.backgroundColor = [UIColor greenColor];
    }
    [self.videosView addSubview:[ [ mediaStream getVideoView ] getView] ];
}

我通过添加更多视图来隐藏它们尝试了不同的方法,但它根本不起作用。如果您已经有解决此问题的方法或解决问题的想法。

1 个答案:

答案 0 :(得分:3)

为显示视频的4个视图中的每个视图设置clipsToBounds属性为YES

view1.clipsToBounds = YES;