iOS:仅绘制UImage的一部分 [英] iOS: Drawing just portions of a UImage

查看:127
本文介绍了iOS:仅绘制UImage的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图只绘制 UIImage 的自定义部分(即:我想揭示 <$ c的部分$ c> UIImage 用户触摸)我使用图层的掩码属性获得了合理的结果

I am trying to draw just a custom portion of a UIImage (i.e.: I would like to reveal the portion of the UIImage user touches) and I am having reasonable results by using mask property of the layer.

我的 UIView 上有类似的东西:

UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath setLineWidth:10.0];
[maskPath moveToPoint:CGPointMake(10.0, 10.0)];
[maskPath addLineToPoint:CGPointMake(100.0, 100.0)];
CAShapeLayer *shapeMaskLayer = [CAShapeLayer layer];
shapeMaskLayer.path = maskPath.CGPath;
[self.layer setMask:shapeMaskLayer];

然后,在 drawRect

- (void)drawRect:(CGRect)rect
{
    [img drawInRect:rect];
}

它有效。我只是看到由 maskPath 定义的图像部分。

It works. I am just seeing the portion of the image defined by maskPath.

然而,看起来不是那个解决这个问题的最佳方法。所以我的问题是:在iOS SDK中绘制图像的自定义部分(可能是任何形状)的最佳方法是什么?

However, doesn't look that that's the best way of approaching this issue. So my question is: What would be the best way of drawing just a custom portion (it could be any shape) of an image in iOS SDK?.

推荐答案

您可以尝试的一件事就是剪切而不是创建额外的图层。例如

One thing you could try is simply clipping instead of creating an extra layer. e.g.

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    UIBezierPath *maskPath = [UIBezierPath bezierPath];
    [maskPath setLineWidth:10.0];
    [maskPath moveToPoint:CGPointMake(10.0, 10.0)];
    [maskPath addLineToPoint:CGPointMake(100.0, 100.0)];

    CGContextAddPath(ctx, maskPath.CGPath);
    CGContextClip(ctx)
    [img drawInRect:rect];
}

这篇关于iOS:仅绘制UImage的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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