![](/img/trans.png)
[英]Why the PNG image resized and shows very well in UIImageView but quality not good when I draw it by CGContextDrawImage
[英]Why does a UIImageView get resized when assigning a new image in iOS 6?
應用程序包含一個包含自定義UITableViewCell的UITableView。 該單元格又包含一個UIImageView。
問題是在cellForRowAtIndexPath中設置圖像會使圖像占用整個UITableViewCell區域:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"bigrect" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
cell.imageView.image = image;
return cell;
}
在IB中,已選擇“Aspect Fit”作為模式,但更改此字段對結果沒有明顯影響。
但是,當從IB設置圖像時,在我的代碼中沒有調用cell.imageView.image = image,結果正是我想要看到的。 圖像保持在我為IB中的UIImageView定義的邊界內,並且不會嘗試縮放以適應UITableViewCell的整個垂直高度:
我正在使用的圖像是1307x309像素,如果這很重要的話。 測試在iOS 6.1模擬器上運行。
我從UIIMageView文檔中注意到了這一點:
在iOS 6及更高版本中,如果為此視圖的restorationIdentifier屬性指定值,則會嘗試保留顯示圖像的幀。 具體來說,該類保留視圖的bounds,center和transform屬性的值以及底層的anchorPoint屬性。 在恢復期間,圖像視圖會恢復這些值,以使圖像與之前完全一致。 有關狀態保存和恢復如何工作的更多信息,請參閱iOS應用程序編程指南。
但是,我能找到的文檔中沒有任何內容可以解決問題。 在“身份”下向IB中的UIImageView添加“Foo”的“恢復ID”並未改變行為。 取消選中“使用Autolayout”也不會改變行為。
在設置圖像時,如何防止iOS在UITableViewCell中調整UIImageView的大小?
事實證明,UITableViewCell顯然已經有一個名為“imageView”的屬性,它覆蓋了整個單元格的背景。 設置此imageView對象的image屬性設置背景圖像,而不是我感興趣的圖像。
將我的方法更改為以下內容,同時確保CustomCell具有“myImageView”屬性修復了問題:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"bigrect" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
cell.myImageView.image = image;
return cell;
}
這個問題回答了一個稍微不同的問題,這使我指出了正確的方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.