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;
}
4

4 回答 4

6

你确定像 Auto-Layout 这样的东西不会在运行时改变你的 imageView 高度吗?尝试像这样设置角半径:

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);
于 2013-10-31T04:55:12.180 回答
1

代替这 2 行代码

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

把它放到代码行

[cell.imageView.layer setCornerRadius:cell.imageView.frame.size.width/2];
 [cell.imageView setClipsToBounds:YES];
于 2013-10-31T09:09:45.180 回答
0

您需要将角半径设置为图像高度的一半,而不是表格单元格。

于 2013-10-31T04:54:45.360 回答
0

尝试这个。

cell.imageView.layer.masksToBounds = YES;

cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2;
于 2013-10-31T05:48:26.550 回答