【问题标题】:iphone stroke path used for clipping UIImage用于裁剪 UIImage 的 iphone 笔划路径
【发布时间】:2014-03-19 04:09:30
【问题描述】:

我对 CoreGraphics 很陌生(虽然我已经做了一段时间的 iphone 编程)

问题,我有这个按比例缩放并将 UIImage 剪辑成圆形的 sn-p:

-(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 个像素)?我需要画一个新的吗?

非常感谢任何帮助!

【问题讨论】:

    标签: iphone uiimage cgcontext cgpath stroke


    【解决方案1】:

    路径可以是任何其他对象 - 在这种情况下,是一个 CGPath (CGPathRef)。在将路径用于任何内容之前,将其封装为 CGPath(在这种情况下,可能是 CGMutablePathRef,或者您可以在使用要剪辑到的路径之前调用 CGContextCopyPath)。现在您可以重用该路径。

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

    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,您可以重用该路径 - 对其进行剪辑,然后对其进行描边。或者反过来。

    【讨论】:

    • 完全是新手,CGContextBeginPath(上下文);有一个无效的回报,我该怎么做你的建议?
    • 我不明白这与它有什么关系。查看 CGMutablePathRef 的文档。另见我的书:apeth.com/iOSBook/ch15.html#_paths_and_drawing
    • 我编辑了我的答案,粘贴了我的一个应用程序中的一些示例代码。请注意,在形成路径后,我可以添加它并对其进行描边,然后再次添加它并对其进行剪辑。
    • 测试它,让我检查一下,我会接受答案,感谢您的帮助 matt!
    • 玩得开心!我真的很喜欢 Core Graphics,很高兴看到你开始参与其中。实时创建 UIImage 绘图的能力是 iOS 的一个非常酷的部分,但没有足够的人利用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2010-09-14
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多