0

I have found the following code from this link: iphone - How to draw a rounded rectangle in Core Graphics / Quartz 2D? - Stack Overflow

So pretty much this is the code:

- (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;

}

I would then call it like this:

myImageView = [self roundImageView:10];

The problem is, how would I return a UIImageView from this code? I have already created a method that takes in a radius parameter and returns an UIImageView. The rect in the code above is already set to original UIImageView's bounds too.

However, I just have no idea how I would return the UIImageView with its rounded corners from this code.

Does anyone have any ideas?

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

3 Answers3

1

Since you begin an image context, and have drawn something on it, I think it would be similar to how you would take a screenshot from the current screen?

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

Then just return a new UIImageView with initWithImage as per normal. However, I don't see there is a need to create a new UIImageView every time you draw the image. You can return UIImage* instead and recycle the UIImageView.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • Where would I put that code in relation to the code I posted? – SimplyKiwi Jun 03 '12 at 03:21
  • @iBradApps: Add it to the end of your code, before the return statement. – nhahtdh Jun 03 '12 at 03:22
  • That's a UIImage, not a UIImageView. So then after that, do this: `UIImageView* ret = [[[UIImageView alloc] initWithImage:image] autorelease]; return ret;`, assuming you rename the UIImage variable to "image". – C0deH4cker Jun 03 '12 at 03:42
0

Add rounded corners to all UIImageViews

UIImage rounded corners

Here are some links that I think are related to your question.

Community
  • 1
  • 1
pasawaya
  • 11,515
  • 7
  • 53
  • 92
0

When you create the UIImageView, try doing this:

UIImageView* imageView = [[[UIImageView alloc] init /*or whatever other init method*/] autorelease];
return imageView;
C0deH4cker
  • 3,959
  • 1
  • 24
  • 35