Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

oo7ml

macrumors 6502
Original poster
Jan 20, 2010
259
0
Hi,

Is it possible to style / display an image as a circle with a 10px border around it in Xcode.

I want to display user's profile image as a circle with a 10px border around it, like this:

Mirage-2.png


Thanks in advance for your help.
 
Added image masking note

Of course you know it is possible as that comes from an app in the app store.

Yes it is possible. You layer your graphics to get the effect and that one would be behind the profile image,

You could build the image in a graphics app and just use that as a lower layer. The problem with that is it isn't very adjustable once in the app.

Personally I'd try to do it in code using Core Graphics. Perhaps the fade in the last third on the left of the circle would need some extra thought but it is doable.

For the image, you use Core Graphics to create a simple mask to get the end result of a circular image. The area outside the circle would be transparent in the resulting rectangle.
 
Last edited:
Hi, thanks for your reply... i have already done the circle image, but not sure how to add a thick 10px white border... any suggestions, thanks again for your help.
 
You could use your image creator/editor to draw the border and make all other pixels transparent. That is, the whole image really would not be round, but if you draw a round border and make sure that's the only part that's visible, that should give you the result you want.
 
He wants to add a border programmatically, I guess.

You can import QuartzCore and then add the border with

Code:
yourView.layer.borderColor = [UIColor whiteColor].CGColor;
yourView.layer.borderWidth = 10.0f;
 
Hi, thanks for your reply... i have already done the circle image, but not sure how to add a thick 10px white border... any suggestions, thanks again for your help.

How did you do the image? Maybe you can take advantage of that knowledge to apply to the circular border.
 
If you want a solid color border then here is a cheap trick that you can place under your image.

Code:
    UIView * circleView = [[UIView alloc] initWithFrame: CGRectMake(100, 400, 100, 100)];
    circleView.layer.backgroundColor = [UIColor clearColor].CGColor;
    circleView.layer.borderColor = [UIColor redColor].CGColor;
    circleView.layer.borderWidth = 10.0;
    circleView.layer.cornerRadius = 50.0;
    circleView.layer.masksToBounds = YES;
    [self.view addSubview: circleView];
 
Couldn't you use a circular .png image with a second (slightly) larger white circle png behind it? .png images have support for transparency. I will have to try it when I get home.

Edit: just tried it, it works fine. Not sure if it would work in your case though, especially if it's supposed to be dynamic.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.