Reputation: 4764
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.
//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
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
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
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