调整UITableView ImageView的大小

时间:2013-11-27 05:09:11

标签: ios objective-c

如何在UITableViewCell中调整UIImageView的大小?我传入一个大图像(至少100x100px),所以图像本身不应该是问题(它应该缩小图像视图)

 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

通常,UITableViewCell中的UIImageView的高度和宽度为43点(或86像素)。我想将它调整到20 x 20,但这没有做任何事情。我错过了什么吗?

2 个答案:

答案 0 :(得分: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();
    

答案 1 :(得分: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; 
}