2013-11-27 132 views
-1

如何在UITableViewCell中調整UIImageView的大小?我傳遞一個大的圖像(至少100x100px),所以圖像本身不應該成爲問題(應該縮小與ImageView的)調整大小UITableView ImageView

cell.imageView.image = [UIImage imageNamed:@"unread.png"]; 


       CGSize itemSize = CGSizeMake(25, 25); 
       UIGraphicsBeginImageContext(itemSize); 
       CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
       [cell.imageView.image drawInRect:imageRect]; 
       cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
       UIGraphicsEndImageContext(); 

enter image description here

通常情況下的UIImageView在UITableViewCell的高度和寬度爲43點(或86像素)。我想將它縮小到20 x 20,但這沒有做任何事情。我錯過了什麼嗎?

+1

爲什麼你沒有使用自定義單元格? –

+0

更好的子類UITableViewCell –

回答

1

您不能更改uitableviewcell的uiimageview的框架。您可以從下面選擇一個選項

  1. 創建自定義單元格並將uiimageview放置在您的要求(這是最好的解決方案,我更喜歡這個)。
  2. 使用核芯顯卡的的cellForRowAtIndexPath裏面:你

    CGSize itemSize = CGSizeMake(width, height); 
    UIGraphicsBeginImageContext(itemSize); 
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
    [cell.imageView.image drawInRect:imageRect]; 
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    
+0

感謝您的迴應。當我嘗試第二種方法時,它調整了imageview的大小,但是我的圖像變成了像素化。這對我來說並沒有多大意義,因爲我縮小了比例。有什麼想法嗎? – Apollo

0

我希望下面的函數非常有用:

+(UIImage*)resizeImage:(UIImage *)image width:(int)width height:(int)height { 

CGFloat targetWidth = width; 
CGFloat targetHeight = height; 

CGImageRef imageRef = [image CGImage]; 
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

if (bitmapInfo == kCGImageAlphaNone) { 
    bitmapInfo = kCGImageAlphaNoneSkipLast; 
} 

CGContextRef bitmap; 

if (image.imageOrientation == UIImageOrientationUp || image.imageOrientation == UIImageOrientationDown) { 
    bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

} else { 
    bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

} 


// In the right or left cases, we need to switch scaledWidth and scaledHeight, 
// and also the thumbnail point 
if (image.imageOrientation == UIImageOrientationLeft) { 
    CGContextRotateCTM (bitmap, radians(90)); 
    CGContextTranslateCTM (bitmap, 0, -targetHeight); 

} else if (image.imageOrientation == UIImageOrientationRight) { 
    CGContextRotateCTM (bitmap, radians(-90)); 
    CGContextTranslateCTM (bitmap, -targetWidth, 0); 

} else if (image.imageOrientation == UIImageOrientationUp) { 
    // NOTHING 
} else if (image.imageOrientation == UIImageOrientationDown) { 
    CGContextTranslateCTM (bitmap, targetWidth, targetHeight); 
    CGContextRotateCTM (bitmap, radians(-180.)); 
} 

CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef); 
CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
UIImage* newImage = [UIImage imageWithCGImage:ref]; 

CGContextRelease(bitmap); 
CGImageRelease(ref); 

return newImage; 
}