2013-10-31 104 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];