UIView中的UIImage并显示图像部分超出范围

时间:2012-09-19 15:16:21

标签: objective-c ios uiview uiimage

我的子视图中的图像比自身的子视图大,是否可以显示超出子视图的图像部分!已经尝试了

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个属性

和backgroundImage foregroundImage

和那个方法

- (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());
}

2 个答案:

答案 0 :(得分:5)

无论如何,视图都无法在其范围之外绘制。设置clipsToBounds只允许在视图外部呈现子视图,但不会改变视图外部的绘图(在drawRect方法中)不可能的事实。

如果您有权访问XRayView来源,请查看图片的呈现方式。它们可能是用drawRect的{​​{1}}方法绘制的。您必须添加UIImageView

答案 1 :(得分:1)

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

时发布的