【发布时间】:2013-08-07 22:11:57
【问题描述】:
我最近问了一个关于在视图的 drawRect 方法中通过路径剪切图像的问题。
Krasnyk 的代码复制如下。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
//or for e.g. CGPathAddRect(path, NULL, CGRectInset([self bounds], 10, 20));
CGPathAddEllipseInRect(path, NULL, [self bounds]);
CGContextAddPath(context, path);
CGContextClip(context);
CGPathRelease(path);
[[UIImage imageNamed:@"GC.png"] drawInRect:[self bounds]];
}
效果很好。但是,当我的图像大于视图本身时,如何显示图像的不同部分?
我尝试调整椭圆和/或 UIImage drawInRect 的位置(如上图所示)的平移,但发生了一些我无法解释的复杂效果(不需要的剪裁、奇怪的椭圆大小)。
编辑:也许我可以回答我自己的问题。我可以尝试 drawAtPoint 而不是 drawInRect 吗?还是drawInRect,将原点设置在不同的位置,但同时扩大矩形的大小?
当我绘制比视图显示的更大的矩形时,这是否会导致性能损失?
【问题讨论】: