user1904273
user1904273

Reputation: 4764

IOS/Objective-C: Code to Make Round Image in Custom Cell

The following code is working fine to make a square image round when placed in cellForRowAtIndexPath.

However, when I move the code to a custom cell, every time the tableview changes, it spreads the circle all the way across the table row into a horizontal blur.

enter image description here

 //following crops and rounds image

    CGSize itemSize = CGSizeMake(40, 40);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);

    [self.iconView.image drawInRect:imageRect];
    self.iconView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.iconView.layer.masksToBounds=YES;
    self.iconView.layer.cornerRadius=20.0;
    self.iconView.frame = CGRectMake(20, 20, 500, 50);

    //   [cell.iconView.image drawInRect:imageRect];
    //   cell.iconView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

In custom cell, I have been placing this code after the following:

self.iconview.image = [UIImage imageNamed:@"test.jpg"];

Does anyone know how I might fix this and still use a custom cell? Thanks in advance for any suggestions.

Upvotes: 0

Views: 292

Answers (3)

Bera Bhavin
Bera Bhavin

Reputation: 703

There is simple way to make rounded image in custom cell:

imageView.layer.cornerRadius = imageDemo.frame.size.width / 2;

imageView.clipsToBounds = YES;

or follow this link

Upvotes: 0

atreat
atreat

Reputation: 4403

There is no need for you to start the image context and get all the way down into CoreGraphics.

I accomplish this by:

imageView.image = myUIImage
imageView.layer.cornerRadius = imageView.bounds.size.width / 2

Upvotes: 0

matt
matt

Reputation: 534925

The issue is what "when I move the code to a custom cell" means - which you do not explain. But the problem is that iconView itself is being resized - it is being made wider. If you don't want that to happen, give it different constraints so that that doesn't happen.

Upvotes: 1

Related Questions