iPhone剪辑图像与路径 [英] iPhone clip image with path

查看:98
本文介绍了iPhone剪辑图像与路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用路径剪辑图像。在使用石英编程一书中,有一个关于如何绘制由矩形路径剪切的圆的示例(第37页),还有一章关于图像蒙版,现有图像为模板(第10章)。但我仍然不确定如何使用路径剪辑现有图像。是否有任何示例或指针?

I would like to clip an image with path. In the book Programming with Quartz there is an example on how to draw a circle clipped by a rectangular path (p.37), and there is also a chapter on image masking with existing image as stencil (Ch.10). But I'm still not sure about how to clip an existing image using path. Is there any example or pointer?

推荐答案

这是一个应该有效的示例,它将剪切区域设置为路径(这在我的情况下,路径是一个elipse,你可以使用rect)。然后绘制将被剪裁的图像。方法drawRect:在我的情况下绘制UIView的上下文。

Here is a sample that should work, it sets the clipping area as a path(this path in my case is an elipse, you could use a rect). Then the image that will be clipped is drawn. Method drawRect: is the one that draws the UIView's context in my case.


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

这篇关于iPhone剪辑图像与路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆