簡體   English   中英

如何調整圖像大小以適應UITableView單元格?

[英]How to resize image to fit UITableView cell?

如何將UIImage放入UITableView ,UITableViewCell(?)的單元格中。

addSubviewcell或是否有辦法來調整cell.imageUIImage之前它被分配給cell.image

我想保持單元格大小默認(無論你使用零矩形初始化時是什么)並且想要為每個條目添加圖片之類的圖片。 圖像略大於單元格大小(表格行大小)。

我認為代碼看起來像這樣(從頭到尾):

UIImage * image = [[UIImage alloc] imageWithName:@"bart.jpg"];
cell = ... dequeue cell in UITableView data source (UITableViewController ...)
cell.text = @"bart";
cell.image = image;

我需要做什么來調整圖像大小以適應單元格大小? 我見過類似的東西:

UIImageView * iview = [[UIImageView alloc] initWithImage:image];
iview.frame = CGRectMake(...); // or something similar
[cell.contentView addSubview:iview]

以上將圖像添加到單元格,我可以計算尺寸以適應它,但是:

  1. 我不確定是否有更好的方法,添加UIImageView只是為了調整cell.image大小是不是太多開銷?
  2. 現在我的標簽( cell.text )需要移動,因為它被圖像遮擋了,我看到了一個解決方案,你只需將文本添加為​​標簽:

例:

UILabel * text = [[UILable alloc] init];
text.text = @"bart";
[cell.contentView addSubview:iview];
[cell.contentView addSubview:label];
// not sure if this will position the text next to the label
// edited original example had [cell addSubview:label], maybe that's the problem

有人能指出我正確的方向嗎?

編輯 :Doh [cell.contentview addSubview:view]不是[cell addSubview:view]也許我應該看看這個:

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = ...;
    CGRect frame = cell.contentView.bounds;
    UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
    myLabel.text = ...;
    [cell.contentView addSubview:myLabel];
    [myLabel release];
}

該網站發布了重新縮放UIImage* 代碼 您可以使用縮放來獲得正在使用的UIImage*的正確比例,以縮小它。

如果您可以訪問apple devloper中心,則會有一個關於縮放圖像視圖的視頻。 它更側重於性能問題並談論很多線程,但他確實展示了一些用於調整圖像大小的示例代碼。

視頻名稱:“有效的iPhone應用程序開發 - 第2部分”約30分鍾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM