2012-12-04 64 views
0

我正在從我的web服務器加載一組圖像以顯示在右側的單元格中。但是,圖像大小不同,因此即使顯示列表,圖像也不會顯示。無論如何,我可以將圖像設置爲像100 x 80這樣的固定大小?xcode ios:設置單元格圖像尺寸

我的代碼遵循本節:

cell.lotImageView.image = [UIImage imageNamed:@"blankthumbnail.png"]; 
cell.lotImageView.clipsToBounds = YES; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 
    //load image from web server 
    NSString *strURL = [NSString stringWithFormat:@"%@/images/%@", user.url, lotPhoto[row]]; 
    NSURL *url = [[NSURL alloc] initWithString:strURL ]; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     // let's make sure the cell is still visible (i.e. hasn't scrolled off the screen) 
     mainTableCell *cell = (mainTableCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     if (cell) 
     { 
      cell.lotImageView.clipsToBounds = YES; 
      cell.lotImageView.image =image; 
     } 
    }); 
}); 
+1

最佳教程,調整大小的圖像與預期的尺寸:http://mobiledevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html –

+0

添加一幀到您的圖像查看cell.lotImageView setFrame:CGrectMake(0,0,100,80)]; – AppleDelegate

回答

0

您可以嘗試調整圖片的大小。此代碼是這樣做的:

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return destImage; 
} 
+0

你的意思是把這段代碼放在.m文件中?不需要打電話嗎? – dan

+0

這是一個方法,所以你需要在你的情況下調用在.m中添加此方法,並更改行UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];到UIImage * image = [self imageWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]] convertToSize:CGSizeMake(100,80)]; – Illuyankas

+0

如果這不起作用,讓代碼和行後添加cell.lotImageView.image = image;此代碼cell.lotImageView setFrame:CGrectMake(cell.lotImageView.frame.origin.x,cell.lotImageView.frame.origin.y,100,80)];通常情況下,您的UIImageView和UIImage尺寸都很大。 – Illuyankas

1

這是您可以爲圖像實現所需大小的一種方法。您可以將下面的函數放入應用程序委託中,並可以在整個應用程序中使用。下面的代碼:

+(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size { 

    CGFloat actualHeight = orginalImage.size.height; 
    CGFloat actualWidth = orginalImage.size.width; 
    if(actualWidth <= size.width && actualHeight<=size.height){ 
     return orginalImage; 
     //NSLog(@"hi thassoods"); 
    } 
    float oldRatio = actualWidth/actualHeight; 
    float newRatio = size.width/size.height; 
    if(oldRatio < newRatio){ 
     oldRatio = size.height/actualHeight; 
     actualWidth = oldRatio * actualWidth; 
     actualHeight = size.height; 
    } 
    else { 
     oldRatio = size.width/actualWidth; 
     actualHeight = oldRatio * actualHeight; 
     actualWidth = size.width; 
    } 
    CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight); 
    UIGraphicsBeginImageContext(rect.size); 
    [orginalImage drawInRect:rect]; 
    orginalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return orginalImage; 
} 

但是,是的,如果你指定的新大小不相稱圖像的原始大小,那麼它可能是可能是您的圖像分辨率不會留下正確的。

所有最好的!