如何减少UItableViewCell图像

时间:2014-08-22 09:15:12

标签: ios objective-c uitableview cgrect

我有来自JSON字符串的网址。我得到了JSON的图像。然后显示在UITableViewCell上。但是我的问题是每个图像的高度和宽度都不相同。

我像这样传递图像数组。

cell.imageView.image = [imagesarray objectAtIndex:indexPath.row];

例如,我得到3张图像,第一张图片尺寸为200 * 200,第二张图片尺寸为80 * 80,线头图片尺寸为120 * 120。

我的问题是:

  • 如何将每张图片修复为80 * 80
  • 如何减少UITableViewCell图像高度和宽度

3 个答案:

答案 0 :(得分:1)

如果您想要固定宽度 - 将图像宽度除以imageView宽度并将其命名为“ratio”。然后按比例划分imageHeight,您将得到imageView所需的高度。然后设置新框架。如何做到这一点可能取决于您的设计(您可能希望更改原点,或保持原样相同)。

对于固定高度,它是相似的。

要设置imageView的大小,请使用

[cell.imageView setFrame:CGRectMake(newX, newY, newWidth, newHeight)];

要调整图片大小而不是imageView,您可以在那里找到帮助:The simplest way to resize an UIImage?

答案 1 :(得分:0)

1缩放图像

一个很好的类别:

#import <UIKit/UIKit.h>

@interface UIImage (Scale)

- (UIImage*)imageScaledToSize:(CGSize)size alpha:(CGFloat)alpha;

- (UIImage*)imageScaledToSize:(CGSize)size;
- (UIImage*)imageScaledToFit:(CGSize)size;
- (UIImage*)imageScaledToFill:(CGSize)size;

@end

实施

@implementation UIImage (Scale)

- (UIImage*)imageScaledToSize:(CGSize)size alpha:(CGFloat)alpha {

    // Create a bitmap graphics context
    // This will also set it as the current context
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); //respect to Retina display

    // Draw the scaled image in the current context
    [self drawInRect:CGRectMake(0, 0, (int)size.width, (int)size.height)
           blendMode:kCGBlendModeMultiply
               alpha:alpha];

    // Create a new image from current context
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    // Pop the current context from the stack
    UIGraphicsEndImageContext();

    // Return our new scaled image
    return scaledImage;
}

- (UIImage*)imageScaledToSize:(CGSize)size {
    return [self imageScaledToSize:size alpha:1];
}

- (UIImage*)imageScaledToFit:(CGSize)size {
    CGFloat scale = size.width / self.size.width;
    scale = MIN(scale, size.height / self.size.height);

    return [self imageScaledToSize:CGSizeMake(self.size.width * scale, self.size.height * scale)];
}

//TODO: crop frame
- (UIImage*)imageScaledToFill:(CGSize)size {
    CGFloat scale = size.width / self.size.width;
    scale = MAX(scale, size.height / self.size.height);

    return [self imageScaledToSize:CGSizeMake(self.size.width * scale, self.size.height * scale)];
}

@end

使用

UIImage *img = [imagesarray objectAtIndex:indexPath.row];
cell.imageView.image =[img imageScaledToFit:CGSizeMake(80, 80)];

设置tableView单元格的大小imageView

[cell.imageView setFrame:CGRectMake(newX, newY, newWidth, newHeight)];
//E.G. [cell.imageView setFrame:CGRectMake(0, 0, 80, 80)];

答案 2 :(得分:-1)

试试这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{         
    UIImage *image=[imagesarray objectAtIndex:indexPath.row];
    CGRect imageRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = [imagesarray objectAtIndex:indexPath.row];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{                 
      UIImage *image=[imagesarray objectAtIndex:indexPath.row];        
      return image.size.height;
 }