【问题标题】:how to Reduces UItableViewCell image如何减少 UItableViewCell 图像
【发布时间】:2014-08-22 09:15:12
【问题描述】:

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

我像这样传递图像数组。

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

例如,我得到了 3 张图像,第一个图像大小为 200*200,第二个图像大小为 80*80,线程图像大小为 120*120。

我的问题是:

  • 如何将每张图片固定为 80*80 尺寸
  • 如何减少UITableViewCell图片的高度和宽度

【问题讨论】:

  • 实现 heightForRowAtIndexPath 并返回 80。它将始终将单元格高度返回为 80。

标签: ios objective-c uitableview cgrect


【解决方案1】:

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

对于固定高度也是类似的。

要设置 imageView 的大小,请使用

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

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

【讨论】:

  • 他想按我的理解缩放图像
  • 我的评论是关于缩放 imageView,这将改变屏幕上图像的大小。但是现在,在新的编辑之后,他可能甚至想改变每个 UIImage 的大小,这在我回答的时候还不清楚。
【解决方案2】:

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)];

2 设置tableView cell imageView的大小

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

【讨论】:

    【解决方案3】:

    试试这个

    - (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;
     }
    

    【讨论】:

    • drawInRect? imageView 那时不应该 drawInRect .. 但除此之外:这根本不起作用
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多