3

I have a weird issue coming up. I am parsing data and pulling out a URL for an image, then setting it to a cell's 'imageView'. From what I can understand, I should set the size of the 'cornerRadius' to half the image's height. Because I am pulling the data into a table view cell with a height of 100, I am setting the corner radius to 50.

Yet when the view load's on the first initial load of the images, they are coming out as small diamonds. When I rotate the device (which rotates the table view too), it automatically brings back the full size image, with the roundness too.

Here's an example image of the initial load,

enter image description here

Does anybody know why this occurs?

Here is my tableView code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *generatedUsers = [self.randomlyGeneratedUsersArray objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:[generatedUsers valueForKeyPath:@"user.picture"]];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [generatedUsers valueForKeyPath:@"user.name.first"], [generatedUsers valueForKeyPath:@"user.name.last"]];
    cell.detailTextLabel.text = [generatedUsers valueForKeyPath:@"user.location.street"];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    cell.imageView.layer.cornerRadius = 50.0f;
    cell.imageView.layer.masksToBounds = YES;

    return cell;
}
1
  • it is right when your cell height is 100.
    – Amitabha
    Oct 31, 2013 at 5:23

4 Answers 4

6

Are you sure something like Auto-Layout isn't changing your imageView height at runtime? try setting the corner radius like this:

cell.imageView.layer.corderRadius = (cell.imageView.bounds.size.height /2);
//And to try figure out what's going on...
NSLog(@"Cell imageview height: %f",cell.imageView.bounds.size.height);
3
  • 3
    +1 was just going to post same, you beat me so didn't post. Cheers. Oct 31, 2013 at 5:00
  • @Dermot So yours gave me the idea to use NSLog to find what the height is being outputted as, and it is returning 0.000000. I've also turned off Autolayout to see if it would change anything at all, and it looks like it hadn't. Oct 31, 2013 at 14:11
  • Hmm, is is your imageView nil? Obj-C can send messages to nil objects without throwing an exception, so your current code would not cause an error if imageView is nil. Try NSLog(@"Image view is %@",(imageView ? @"not nil" : @"nil"));
    – Dermot
    Nov 1, 2013 at 0:09
1

In the place of this 2 line of code

    cell.imageView.layer.cornerRadius = 50.0f;
    cell.imageView.layer.masksToBounds = YES; 

Put this to line of code

[cell.imageView.layer setCornerRadius:cell.imageView.frame.size.width/2];
 [cell.imageView setClipsToBounds:YES];
0

You need to set the corner radius to be half the height of the image, not the table cell.

0

try this.

cell.imageView.layer.masksToBounds = YES;

cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2;
2
  • This was something I had been trying, but because the imageView bounds size height is returning 0, it doesn't work. I'm now trying to figure out why that is. Oct 31, 2013 at 14:13
  • try setting contentmode property of imageView.
    – Vaisakh
    Nov 1, 2013 at 3:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.