我想在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;
}
答案 0 :(得分:0)
假设您的对象视图是方形的,请将cornerRadius
设置为宽度或高度的一半。
maskToBounds
根据圆形imageView
例如,根据您的要求添加此代码,
yourImageView.layer.cornerRadius = yourImageView.imageView.frame.size.height /2;
yourImageView.layer.masksToBounds = YES;
希望这会对你有所帮助:)。