Mukesh Mukesh - 9 months ago 34
iOS Question

How can i set image circular

I have done following code but it does not work. It shows me in circle but in crop image format.

I want to fit image to circle.

UIImage * defaultImage = [UIImage imageNamed:@"empty.png"];
self.myImageView = [[UIImageView alloc] initWithImage:defaultImage];

CGRect myFrame = CGRectMake(90.0f, 100.0f, self.myImageView.frame.size.width/1.5,self.myImageView.frame.size.height/1.5);
[self.myImageView setFrame:myFrame];

[self.myImageView setContentMode:UIViewContentModeScaleAspectFit];

self.myImageView.layer.masksToBounds = NO;
self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width/2;
self.myImageView.layer.borderColor = [UIColor blackColor].CGColor;
self.myImageView.clipsToBounds=YES;

self.myImageView.layer.borderWidth = 2.0f;

[self.view addSubview:self.myImageView];

Answer Source

You can use this method

-(void)setRoundedView:(UIImageView *)roundedView toDiameter:    
(float)newSize;
{
    CGPoint saveCenter = roundedView.center;
    CGRect newFrame = CGRectMake(roundedView.frame.origin.x, 
    roundedView.frame.origin.y, newSize, newSize);
    roundedView.frame = newFrame;
    roundedView.layer.cornerRadius = newSize / 2.0;
    roundedView.center = saveCenter;
}

Now call the method with your imageview

[self setRoundedView:self.myImageView toDiameter:self.myImageView.frame.size.width];
 self.myImageView.clipsToBounds = YES;

Hope it helps