2014-02-19 43 views
1

我遇到了我的tableView UIImages渲染問題,並想知道是否有人遇到同樣的問題,並知道如何解決它。表視圖UIImage渲染問題

這裏是我的cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.textLabel.text = exerciseDisplayName; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 

    [tableView setSeparatorInset:UIEdgeInsetsZero]; 

    UtilityMethods *commonMethods = [[UtilityMethods alloc]init]; 

    UIImage *rowImage = [commonMethods imageForRow:tempPlaceholder.bodyPart]; 

    cell.imageView.image = rowImage; 

    return cell; 

} 

這裏是我行高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath     
    { 
    return 96; 
    } 

enter image description here

有許多在表中的圖像線條和波浪線的。我想知道是否有人知道我可能需要應用到我的圖像來解決問題的任何UIImage屬性。增加表格中行的高度以增加表格行高度爲代價來修復問題。似乎工作的數字是128 heightForRow。使用128時,波形不太明顯。現在我很確定這與iOS呈現圖像的方式有關。我採取了形象,並使用微軟油漆調整到76x76只是爲了看看我是否會看到同樣的問題,並且圖像顯示沒有所有的歪曲。這些圖像是.png格式。這些圖像的原始大小是1024x1024。我只是根據需要調整了它們的大小。如果任何人有任何提示或建議如何解決這個問題,我真的很感激它。

+0

向下調整圖像大小的唯一原因是爲了節省空間。無論圖像視圖的大小是以像素爲單位的,您加載的圖像的大小至少應爲該圖像的兩倍。 – nhgrif

+0

我試着把表格視圖圖像的1024x1024格式留下來。當行的高度爲96時,問題仍然存在。將行高增加到128可緩解問題。 – zic10

回答

2

您將需要重新採樣圖像到您需要的大小。在iOS設備上(大多數情況下),在小空間查看大圖像看起來相當糟糕。但是,如果使用內置函數來創建適當大小的新UIImage,那麼所有內容都會更好。在顯示時縮小UIImage總是看起來比創建適當大小的新圖像並顯示更差。要做到這一點的方法是如下(taken from here)

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 
{ 
    UIImage *sourceImage = self; 
    UIImage *newImage = nil;  
    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 
    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    { 
     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor > heightFactor) 
     { 
      scaleFactor = widthFactor; // scale to fit height 
     } 
     else 
     { 
      scaleFactor = heightFactor; // scale to fit width 
     } 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // center the image 
     if (widthFactor > heightFactor) 
     { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else 
     { 
      if (widthFactor < heightFactor) 
      { 
       thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
      } 
     } 
    } 

    UIGraphicsBeginImageContextWithOptions(targetSize, 0, NO); // this will crop 

    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    if(newImage == nil) 
    { 
     NSLog(@"could not scale image"); 
    } 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

,這個功能並多一點比你所尋找的,但你應該能夠削減它對你只需要什麼。

請務必使用UIGraphicsBeginImageContextWithOptions功能而不是UIGraphicsBeginImageContext,以便正確處理視網膜顯示,否則會使圖像比他們應該更模糊,並且還會有第二個問題需要處理。

+0

謝謝,這工作完美。 – zic10