我有一個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;
我試圖迫使UIImageView
是在通過設置原點0,0幀,但這並沒有幫助,我現在在想,這可能是該圖像被調整的事實,那麼UITableViewCell
中的某些內容會中斷,但不知道它可能是什麼。對此的一些援助將非常感謝。
另請注意,我將所有這些視爲故事板,並且它具有完全相同的結果,然後我想讓我以編程方式嘗試並獲得相同的結果。
在單元格中添加UIImageView,並且不要使用單元格默認的imageview ...這將解決問題... – 2014-10-19 07:12:43
@FahimParkar我沒有使用默認圖像視圖 – Armand 2014-10-19 07:23:45
@FahimParkar好,所以我重新檢查,正如你所說,我是使用錯誤的imageview,感謝兄弟 – Armand 2014-10-19 07:35:45