unable to set the image view to be circle - ios

i need to make the profile photo image circle (similar to whats app)
The cell i use is UITableViewCellStyleDefault
here's my code:
User *user = [DBHelper fetchUserWithUserID:userID];
cell.imageView.image = [Helper imageWithBlob:user.profile.thumbnailPhotoBlob defaultImageFile:FILE_DEFAULT_PHOTO];
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height /2;
cell.imageView.clipsToBounds = YES;
cell.imageView.layer.masksToBounds = YES;
But the result image view is still a square
EDIT: The image view's frame is {0, 0, 0, 0}, but it did show the image.
EDIT 2:
I tried the answer but the image is not circle. Here's what i did:
float height = [self tableView:tableView heightForRowAtIndexPath:indexPath];
[Helper circlizeImageView:cell.imageView cellHeihgt:height];
+ (void)circlizeImageView:(UIImageView *)imageView cellHeihgt:(float)cellHeight {
imageView.frame = CGRectMake(0, 0, cellHeight, cellHeight);
[Helper circlizeView:imageView];
}
+ (void)circlizeView:(UIView *)view {
view.layer.cornerRadius = view.frame.size.height / 2;
view.clipsToBounds = YES;
view.layer.masksToBounds = YES;
}

The code is all fine.
The only problem is with this line:
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2;
//CGRect rectTest = cell.imageView.frame;
//returns a rect of (0,0,0,0) & hence the cornerRadius being set is 0.
Try:
cell.imageView.layer.cornerRadius = 10; //or some fixed value
In retrospect, what you're trying to do will not get you to make the imageView circular because the default imageView is rectangular (the height is always more than the width) and so... setting a corner radius that is half of the height/width will not work.
It'll be better if you use a custom UITableViewCell and add a square framed imageView.
ANyways... Since the default imageView touches the top of the default UITableViewCell and ends at the bottom, you can get the height of the imageView by using the height of the row (in case you have dynamically sized cells)
So, something as simple as this:
cell.imageView.layer.cornerRadius = cell.frame.size.height/2;
But none of this looks good since the imageView is rectangularish and cells being of dynamic height will make it look like a poor design (the fixed radius looks best imho)

You have two options here:
1- Add the imageView as a subView to you cell.
2- Override layoutSubviews in a custom cell:
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,40,40);
}

Related

Autolayout with dynamic height

I am trying to create a subclass of UIView that will show a list of fixed sized UIImages similar to how a UILabel displays letters.If all the images won't fit on one line, the images are arranged on multiple lines.
How can I achieve this using autolayouts so that if I put this view in a UIStackView the images will be listed correctly?
Here is a sample if I did it using fixed position :
- (void) layoutSubviews{
[super layoutSubviews];
CGRect bounds = self.bounds;
CGRect imageRect = CGRectMake(0, 0, 30, 30);
for (UIImageView* imageView in self.imageViews){
imageView.frame = imageRect;
imageRect.origin.x = CGRectGetMaxX(imageRect);
if (CGRectGetMaxX(imageRect) > CGRectGetMaxX(bounds)){
imageRect.origin.x = 0.0;
imageRect.origin.y = CGRectGetMaxY(imageRect);
}
}
}
Update:
Here is a sample project to show the issue.
https://github.com/datinc/DATDemoImageListView
Here the link for ImageListView
https://github.com/datinc/DATDemoImageListView/blob/master/DATDemoImageListView/DATDemoImageListView.m
You should overwrite intrinsicContentSize returning the preferred content size of your view.
The problem is that in intrinsicContentSize the view has not it's final bounds. You can use an internal height constraint, and overwrite updateConstraints:
- (void)updateConstraints {
CGFloat theWidth = CGRectGetWidth(self.bounds);
NSUInteger theCount = [self.subviews count];
CGFloat theRows = ceilf(theCount / floorf(theWidth / 30.0));
self.heightConstraint.constant = 30.0 * theRows;
[super updateConstraints];
}
In viewDidAppear: and on layout changes (e. g. rotation) you have to call setNeedsUpdateConstraints to get a proper initial layout of the image views.

custom uitableviewcell reset subview frame doesn't work as I wanted

I custom a uitableviewcell with nib, and want to custom the highlighted style. When the cell highlighted, I want to reduce the size of the subview of the cell and keep the position of the imageview in the cell not changing. So, it looks like the frame of a imageview zoomed out, but the image itself stay there not changing its position.
However, this code snippet doesn't work as I wanted.Could anybody help me to figure out where I am wrong. Any help will be appreciated!
tips: the imageview is a subview of self.frameView and frameView is a subview of frameBgView.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted)
{
self.frameView.backgroundColor = UIColorFromRGBA(0, 0, 0, 0.1);
CGRect rect = self.frameBgView.frame;
rect.origin.x += 10;
rect.size.width -= 20;
self.frameBgView.frame = rect;
rect = self.frameView.frame;
rect.origin.x -= 10;
self.frameView.frame = rect;
}
else
{
....
}
}
EDIT: some screenshots to explain the question:
oky i tried your solution but i got like this i am posting the code as well as the output how it looks, if there is any problem just comment , i am deleted the frameBgView it is not required
code
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted)
{
self.frameView.backgroundColor = [UIColor grayColor];
CGRect rect = self.contentView.bounds;
rect.origin.x += 15;
rect.size.width -= 30;
//rect = self.frameView.frame;
// rect.origin.x -= 10;
self.frameView.frame = rect;
}
else
{
self.frameView.backgroundColor = [UIColor whiteColor];
CGRect rect = self.contentView.bounds;
rect.origin.x += 10;
rect.size.width -= 20;
self.frameView.frame = rect;
}
}
and i am not using auto layout .. the result what i got is, before
and after highlighted,
EDIT:
this is the cell structure
as u can see in the picture, in content view i hav added frameView which is blue in colour, and within frameView i hav added imageView and also don't forget t set content mode scale to fill and also auto resizing masks for both frameView and imageView for example
autoresizing masks for content view
autoresizing masks for frameView
autoresizing masks for imageView
END EDIT

How to implement this margin for UITableViewCell via layers

I need to implement this left margin for the background of the cell.
Now I tried to do it this way - but the result is not correct - the content of the cell is moving with the margin. How can I solve an issue?
CGRect layerFrame = self.layer.frame;
layerFrame.size.width -= kRightLayerMargin;
layerFrame.origin.x += kLeftLayerMargin;
self.layer.frame = layerFrame;
self.layer.backgroundColor = [[SCUtils cellSelectionColor] CGColor];
self.layer.cornerRadius = 10.0f;
Implement your cell's layoutSubviews method as follows:
- (void)layoutSubviews {
//have the cell layout normally
[super layoutSubviews];
//get the bounding rectangle that defines the position and size of the textLabel
CGRect textLabelFrame = [[self textLabel] frame];
//anchor it to the top-left corner
textLabelFrame.origin = CGPointMake(kLeftLayerMargin, textLabelFrame.origin.y);
//set the textLabel frame (this will move+resize it)
[[self textLabel] setFrame:textLabelFrame];
//reposition the other controls accordingly
}

UIImageView and autolayout

I have a view that is set up nicely using autolayout. The view contains a series of labels stacked from top to bottom. I am allowing the intrinsic size of these labels to determine the size of the view.
The final step is to add a background from an image. I started by trying the colorWithPatternImage method on UIColor but this isn't quite what I am looking for. I do not want to tile the image, and I can not guarantee it will always be larger than the intrinsic size of the view.
Similarly, adding a uiImageView to the view itself doesn't quite work. The view will expand to accommodate the image when I want to keep the intrinsic size based on the labels.
I guess what I am looking for is the following.
1) The background should have no effect on the size of the view.
2) The image should be scaled to fill the view but in it's original aspect ration (so cropping edges if necessary).
Any ideas appreciated.
In my case, I needed it for a UIImageView inside a dynamically-sized view in a UITableViewCell, but the image refused to shrink below its instristic size and instead worked as a minimum-size constraint for the superview. The only way I could get it ignore the intristic size is by lowering the priority at which it is enforced, right after creating the cell:
[imageView setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[imageView setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisVertical];
After this, all my constraints magically started working. In the OP's case, setting UIViewContentModeScaleAspectFill is also required, as per Mundi's answer.
In Interface Builder, add a UIImageView as the first subview to the view. Make sure its size always matches the view.
Then, in Interface Builder or code, set the contentMode:
backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
Here's how I would approach this. Hopefully it helps. :)
CGRect contentFrame = CGRectMake(0, 0, self.view.frame.size.width, 0); // This will be the frame used to create the background image view.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(20, 20, 20, 20); // The margins by which the labels will be inset from the edge of their parent view.
CGFloat labelHeight = 21;
CGFloat verticalGap = 8; // The vertical space between labels
CGFloat y = contentInsets.top;
int numberOfLabels = 10;
for (int i = 0; i < numberOfLabels; i++) {
CGRect frame = CGRectMake(contentInsets.left, y, self.view.frame.size.width - (contentInsets.left + contentInsets.right), labelHeight);
UILabel *label = [[[UILabel alloc] initWithFrame: frame] autorelease];
// customize the label here
[self.view addSubview: label];
contentFrame = CGRectUnion(contentFrame, label.frame);
y += labelHeight + verticalGap;
}
contentFrame.size.height += contentInsets.bottom;
UIImageView *backgroundImageView = [[[UIImageView alloc] initWithFrame: contentFrame] autorelease];
[backgroundImageView setClipsToBounds: YES];
[backgroundImageView setContentMode: UIViewContentModeScaleAspectFill];
[backgroundImageView setImage: [UIImage imageNamed: #"background_image.png"]];
[self.view insertSubview: backgroundImageView atIndex: 0];

Setting the size for an image in a TableViewCell

I have a table with rows of height 90.
I want to have an image on each row of size 50x50.
I am doing the following in cellForRowAtIndexPath:
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
if (item.image){
cell.imageView.image = [item.image smallImage];
cell.imageView.alpha = 1.0f;
} else {
cell.imageView.image = [UIImage imageNamed:#"icon-greyed.png"];
cell.imageView.alpha = 0.25f;
}
However the images appear with height 90 (the height of the row). What am I not doing right?
You will need to either add a UIImageView to cell.contentView which I wouldn't recommend, or create a custom UITableViewCell, which I would recommend. Then you can add a UIImageView to your custom cell and resize it to the dimensions you require.

Resources