【问题标题】:UIImage in UIView and display image parts out of boundsUIView 中的 UIImage 并显示图像部分越界
【发布时间】:2012-09-19 15:16:20
【问题描述】:

我在子视图中有一个比它自己的子视图更大的图像,是否可以显示子视图之外的部分图像!试过了

view.clipsToBounds=NO; 

但仍然不缺。

到目前为止,这是我的代码的一部分。

xRayView = [[XRayView alloc] initWithFrame: CGRectMake((1024 - 800) / 2, self.device.frame.origin.y - 26, 800,530)];
        xRayView.clipsToBounds=NO;
        [xRayView setForgroundImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"xray_foreground_01" ofType: @"png"]]];
        [xRayView setBackgroundImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"xray_background_01" ofType: @"png"]]];

前景和背景图像大于 X 射线视图

在 XrayView 中我有 2 个属性

背景图片 前景图像

还有那个方法

- (void)drawRect: (CGRect)rect
{
    [super drawRect: rect];

    CGRect forgroundClips[] = {
        CGRectMake((self.frame.size.width - self.forgroundImage.size.width)/2, 0, self.forgroundImage.size.width, currentPan.origin.y),
        CGRectMake((self.frame.size.width - self.forgroundImage.size.width)/2, currentPan.origin.y + currentPan.size.height, self.forgroundImage.size.width, self.forgroundImage.size.height - (currentPan.origin.y + currentPan.size.height))
    };

    CGContextSaveGState(UIGraphicsGetCurrentContext());
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, self.bounds.size.height);
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
    CGContextClipToRects(UIGraphicsGetCurrentContext(), forgroundClips, sizeof(forgroundClips) / sizeof(forgroundClips[0]));
    //CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height), self.forgroundImage.CGImage);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake((self.frame.size.width - self.forgroundImage.size.width)/2, 0, self.forgroundImage.size.width, self.forgroundImage.size.height), self.forgroundImage.CGImage);
    CGContextRestoreGState(UIGraphicsGetCurrentContext());

    CGContextSaveGState(UIGraphicsGetCurrentContext());
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, self.bounds.size.height);
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
    CGContextClipToRect(UIGraphicsGetCurrentContext(), currentPan);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 153, 153, 153, 0.5);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3);
    CGContextStrokeRect(UIGraphicsGetCurrentContext(), currentPan);
    //CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height), self.backgroundImage.CGImage);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake((self.frame.size.width - self.backgroundImage.size.width)/2 , 0, self.backgroundImage.size.width, self.backgroundImage.size.height), self.backgroundImage.CGImage);
    CGContextRestoreGState(UIGraphicsGetCurrentContext());

    // Draw a yellow hollow rectangle

   // CGContextRestoreGState(UIGraphicsGetCurrentContext());
}

【问题讨论】:

  • 尝试为包含forgroundImage [SIC] 和backgroundImageUIImageView 对象设置clipToBounds 属性。如果还不行,您可能需要向我们提供有关 XRayView 的视图层次结构的更多详细信息。

标签: objective-c ios uiview uiimage


【解决方案1】:

无论如何,视图都不能超出其边界。设置clipsToBounds 将只允许子视图在视图之外呈现,但它不会改变无法在视图之外绘制(在drawRect 方法中)的事实。

如果您有权访问XRayView 源,请查看图像是如何呈现的。它们可能是在XRayViewdrawRect 方法中绘制的。您必须改为添加UIImageView

【讨论】:

    【解决方案2】:

    clipsToBounds 表示图像将被裁剪到边界之外。确保它设置为NO。 (这是在原帖说clipsToBounds = YES;时发的)

    【讨论】:

    • 是的,对不起,我的错误设置为 NO,但仍然有问题!
    • 我认为可能是因为您使用这些背景图像,进入 xRayView 并确保 backgroundImage(view) 和 foregroundImage(view) 设置为clipsToBounds=NO
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多