如何在UIImageView上绘制圆圈而不在objective-c中添加图层?

时间:2017-01-04 03:42:25

标签: ios objective-c uiimageview draw

我想在UIImageView上画一个圆圈。我试过了,但它没有用。

这是我想要实现的示例图像:

应该在用户点击UIImageView的位置绘制圆圈,我想在不添加任何sublayer的情况下进行操作。

这是一种方法吗? 到目前为止,我已经从互联网上使用了这个代码,但它没有用。

 - (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image
                                  pointX:(float) x
                                 PointY:(float) y
{
    // begin a graphics context of sufficient size
    UIGraphicsBeginImageContext(image.size);

    // draw original image into the context
    [image drawAtPoint:CGPointZero];

    // get the context for CoreGraphics
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // set stroking color and draw circle
    [[UIColor redColor] setStroke];

    // make circle rect 5 px from border
    CGRect circleRect = CGRectMake(0, 0,
                                   image.size.width,
                                   image.size.height);
    circleRect = CGRectInset(circleRect, x, y);

    // draw circle
    CGContextStrokeEllipseInRect(ctx, circleRect);

    // make image out of bitmap context
    UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

    // free the context
    UIGraphicsEndImageContext();
    return retImage;
}

1 个答案:

答案 0 :(得分:0)

假设您的对象视图是方形的,请将cornerRadius设置为宽度或高度的一半。 maskToBounds根据圆形imageView

的形状设置图片

例如,根据您的要求添加此代码,

yourImageView.layer.cornerRadius = yourImageView.imageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;

希望这会对你有所帮助:)。