2014-10-19 40 views
0

我有一個UITableView,我編程添加到我的看法。然後我從API加載數據,並調整圖像的大小以適應當前設備的寬度,並相應地調整圖像的高度以保持寬高比,然後使用該高度作爲單元格的高度我的桌子視圖。UIImageView有一個奇怪的空間在圖像左邊的圖表

下面是一些代碼,並將結果的屏幕截圖:

這是圖像尺寸變換代碼:

+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width { 
    float ratio = image.size.width/width; 
    float height = image.size.height/ratio; 
    CGSize size = CGSizeMake(width, height); 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newimage; 
} 

這是在我的定製表格視圖細胞的init方法:

_imgPicture = [[UIImageView alloc] init]; 
_imgPicture.contentMode = UIViewContentModeScaleAspectFit; 
[self.contentView addSubview:_imgPicture]; 

這是我的cellForRowAtIndexPath

RecipeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"recipe_cell"]; 

if(cell == nil) 
{ 
    cell = [[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"recipe_cell"]; 
} 
NSDictionary *dict = [recipeArray objectAtIndex:[indexPath row]]; 
cell.lblName.text = [dict valueForKey:@"name"]; 

if([@"data:image/jpg;base64,null" isEqualToString:[NSString stringWithFormat:@"%@,%@",[dict valueForKey:@"pictureType"], [dict valueForKey:@"picture"]]]) 
{ 
    cell.imgPicture.image = defaultImage; 
} 
else 
{ 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@,%@",[dict valueForKey:@"pictureType"], [dict valueForKey:@"picture"]]]; 
    NSData *imageData = [NSData dataWithContentsOfURL:url]; 
    UIImage *ret = [UIImage imageWithData:imageData]; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 
    cell.imageView.image = [ImageExtension imageWithImage:ret convertToWidth:screenWidth]; 
} 

return cell; 

Screen shot of the resulting view

我試圖迫使UIImageView是在通過設置原點0,0幀,但這並沒有幫助,我現在在想,這可能是該圖像被調整的事實,那麼UITableViewCell中的某些內容會中斷,但不知道它可能是什麼。對此的一些援助將非常感謝。

另請注意,我將所有這些視爲故事板,並且它具有完全相同的結果,然後我想讓我以編程方式嘗試並獲得相同的結果。

+1

在單元格中添加UIImageView,並且不要使用單元格默認的imageview ...這將解決問題... – 2014-10-19 07:12:43

+0

@FahimParkar我沒有使用默認圖像視圖 – Armand 2014-10-19 07:23:45

+0

@FahimParkar好,所以我重新檢查,正如你所說,我是使用錯誤的imageview,感謝兄弟 – Armand 2014-10-19 07:35:45

回答

1

變化的壓痕

cell.imageView.image 
    ^^^^^^^^^ 

cell.imgPicture.image 
    ^^^^^^^^^^ 

這將解決問題...

0

第一u能如果使用iOS8上再看到你constraits 第三是給予的tableview任何的backgroundColor 其次,確保圖像的大小調整,如果有的話IOS然後檢查的UITableViewCell

+0

任何想法約束檢查,它是所有生成編程,我已經添加了一個背景顏色的細胞和細胞按預期填充整個寬度。我添加了'cell.indentationLevel = 0;'和'cell.contentView.frame = CGRectMake(0.0f,0.0f,screenWidth,[cell.imageView.image size] .height);'我也嘗試從單元格的內容視圖 – Armand 2014-10-19 07:32:34