Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage in a circle

Tags:

Can somebody help me here?. New as iPhone Developer. I am trying to display a .png picture in a circle instead of a rectangle which is the standard for iPhone

like image 716
Wilmer Avatar asked Dec 10 '10 23:12

Wilmer


1 Answers

Well, all png files are 'rectangles' but if you want to have the apperence of a circle or other non rectangle object on the screen you can do so by using transparacy. To make sure the transparent pixels in the image are also transparent on the iPhone, you can set the background color of the UIImageView to clear. This can be done in Interface Builder by dragging the opacity slider in the background color picker all the way down, or in code as follows:

UIImage *image = [UIImage imageNamed:@"yourRoundImage.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.backgroundColor = [UIColor clearColor]; [self.view addSubview: imageView]; 

If you simply want to add rounder corners, to make a circle, you can also use the cornerRadius Property like this if you have added the QuartzCore framework to your project:

#import <QuartzCore/QuartzCore.h> UIImage *image = [UIImage imageNamed:@"yourRoundImage.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.layer.cornerRadius = image.size.width / 2; imageView.layer.masksToBounds = YES; [self.view addSubview: imageView]; 
like image 200
Brad The App Guy Avatar answered Sep 19 '22 19:09

Brad The App Guy