0

我从这个链接中找到了以下代码:iphone - How to draw a rounded rectangles in Core Graphics / Quartz 2D?- 堆栈溢出

几乎这是代码:

- (UIImageView*)roundImageView:(CGFloat)rad {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 

    CGRect rrect = ivprofilePicture.bounds;

    CGFloat radius = rad; 

    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect); 

    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect); 

    CGContextMoveToPoint(context, minx, midy); 

    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); 

    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); 

    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); 

    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); 

    CGContextClosePath(context); 

    CGContextDrawPath(context, kCGPathFillStroke);



    UIImageView *imageView;

    return imageView;

}

然后我会这样称呼它:

myImageView = [self roundImageView:10];

问题是,我如何从这段代码返回 UIImageView ?我已经创建了一个接收半径参数并返回 UIImageView 的方法。上面代码中的矩形也已经设置为原始 UIImageView 的边界。

但是,我只是不知道如何从这段代码中返回带有圆角的 UIImageView。

有没有人有任何想法?

谢谢!

4

3 回答 3

1

既然你开始了一个图像上下文,并在上面画了一些东西,我认为它类似于你从当前屏幕截取屏幕截图的方式?

UIImage* imageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

initWithImage然后按照正常返回一个新的 UIImageView 。但是,我认为每次绘制图像时都不需要创建一个新的 UIImageView 。您可以返回UIImage*并回收 UIImageView。

于 2012-06-03T03:18:42.933 回答
0

创建 UIImageView 时,请尝试执行以下操作:

UIImageView* imageView = [[[UIImageView alloc] init /*or whatever other init method*/] autorelease];
return imageView;
于 2012-06-03T02:58:12.440 回答
0

为所有 UIImageViews 添加圆角

UIImage圆角

以下是我认为与您的问题相关的一些链接。

于 2012-06-03T02:54:08.947 回答