2013-10-31 86 views
3

我有一个奇怪的问题来了。我正在解析数据并提取图片的网址,然后将其设置为单元格'imageView'。根据我的理解,我应该将'cornerRadius'的大小设置为图像高度的一半。因为我正在将数据拉入高度为100的表格视图单元格中,因此我将角部半径设置为50.尝试一个圆形图像,输出一个钻石形状?

然而,当视图加载在图像的第一次初始加载时,它们会以小钻石。当我旋转设备(也旋转桌面视图)时,它会自动恢复全尺寸图像,同时还具有圆度。

这里的初始负载的示例图像,

enter image description here

有谁知道为什么出现这种情况?

这里是我的tableView代码

- (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; 
} 
+0

它是正确的,当你的身高是100 – Amitabha

回答

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); 
+3

+1只是将张贴相同,你打我,所以没有发布。干杯。 –

+0

@Dermot所以你给了我想法,使用NSLog来查找正在输出的高度,它返回0.000000。我也关闭了Autolayout,看它是否会改变任何事情,而且看起来没有。 –

+0

嗯,是你的imageView零? Obj-C可以将消息发送给nil对象而不会抛出异常,因此如果imageView为零,那么当前的代码不会导致错误。试试NSLog(@“Image view is%@”,(imageView?@“not nil”:@“nil”)); – Dermot

0

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

0

试试这个。

cell.imageView.layer.masksToBounds = YES; 

cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height/2; 
+0

这是我一直在尝试的东西,但因为imageView边界大小高度返回0,它不起作用。我现在试图弄清楚为什么。 –

+0

尝试设置imageView的contentmode属性。 – Vaisakh

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];