用于剪辑UIImage的iPhone笔触路径 [英] iphone stroke path used for clipping UIImage

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

问题描述

我是CoreGraphics的新手(尽管我已经做过一段时间的iphone编程工作了)

I'm very new to CoreGraphics (although I've been doing iphone programming for a while)

问题,我有此代码段可以按比例缩放并裁剪UIImage到一个圆圈:

Question, I have this snippet that scales proportionally and clips an UIImage to a circle:

-(UIImage *)counterpartImageForSchedule:(Schedule *)counterpartSchedule inSize:(CGSize)size
{
    // This function returns a newImage, based on image, that has been:
    // - scaled to fit in (CGRect) rect
    // - and cropped within a circle of radius: rectWidth/2

    UIImage *image=[UIImage imageNamed:@"fakeuser1.png"];

    // when kendy sends hash, check that this image is not on the cache, otherwise download, clip & stylized
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();


                //Get the width and heights
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    CGFloat rectWidth = size.width;
    CGFloat rectHeight = size.height;

    //Calculate the scale factor
    CGFloat scaleFactorX = rectWidth/imageWidth;
    CGFloat scaleFactorY = rectHeight/imageHeight;

    if (scaleFactorX>scaleFactorY) {
        scaleFactorY=scaleFactorX;
    } else
    {
        scaleFactorX=scaleFactorY;
    }


    //Calculate the centre of the circle
    CGFloat imageCentreX = rectWidth/2;
    CGFloat imageCentreY = rectHeight/2;

    // Create and CLIP to a CIRCULAR Path
    // (This could be replaced with any closed path if you want a different shaped clip)
    CGFloat radius = rectWidth/2;
    CGContextBeginPath (context);
    CGContextAddArc (context, imageCentreX, imageCentreY, radius, 0, 2*M_PI, 0);


    CGContextClosePath (context);

    CGContextClip (context);

    //Set the SCALE factor for the graphics context
    //All future draw calls will be scaled by this factor
    CGContextScaleCTM (context, scaleFactorX, scaleFactorY);    
    // the stroke

    // Draw the IMAGE
    CGRect myRect = CGRectMake(0, 0, imageWidth, imageHeight);
    [image drawInRect:myRect];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();



    return newImage;
}

问题是,我该如何画出剪切路径图像(例如4个黑色或白色像素)?我需要画一个新的吗?

Question is, how can I stroke the path I'm using to clip the image (say, 4 pixels with black or white color)? do i need to draw a new one?

非常感谢您的帮助!

推荐答案

路径可以是任何其他对象,在这种情况下,可以是CGPath(CGPathRef)。在使用任何路径之前,请将其封装为CGPath(在这种情况下,可能是CGMutablePathRef,或者也许可以在使用路径进行剪切之前调用CGContextCopyPath)。现在您可以重用该路径了。

A path can be an object like any other - in this case, a CGPath (CGPathRef). Before using the path for anything, encapsulate it as a CGPath (in this case, probably a CGMutablePathRef, or perhaps you can call CGContextCopyPath before using the path to clip to). Now you can reuse that path.

下面是我的一个应用程序中的一个示例,我在其中形成了一条路径,然后对其进行描边,然后剪切到相同的路径( c 是图形上下文):

Here's an example from one of my apps where I form a path, then stroke it, and then clip to the same path (c is the graphics context):

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, CGRectGetMaxX(r) - radius, ins);
CGPathAddArc(path, nil, 
             radius+ins, radius+ins, radius, -M_PI/2.0, M_PI/2.0, true);
CGPathAddArc(path, nil, 
             CGRectGetMaxX(r) - radius, radius+ins, radius, M_PI/2.0, -M_PI/2.0, true);
CGPathCloseSubpath(path);
CGContextAddPath(c, path);
CGContextSetLineWidth(c, 2);
CGContextStrokePath(c);
CGContextAddPath(c, path);
CGContextClip(c);
CGPathRelease(path);

另一种可能性是使用UIBezierPath-一个成熟的Objective-C对象,而不是CGContext函数。它封装了一个CGPath,您可以重用该路径-对其进行剪辑,然后对其进行描边。或反过来。

Another possibility is to use UIBezierPath - a full-fledged Objective-C object, instead of CGContext functions. It encapsulates a CGPath and you can reuse that path - clip to it, then stroke it. Or the other way round.

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

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