【问题标题】:Downloaded image has bigger size than wanted下载的图像比想要的大
【发布时间】:2015-02-25 07:42:00
【问题描述】:

问题

我正在开发一个应用程序,我从上传到网站的 JSON 文件下载数据。下载的所有内容都正确显示...但是我有这个大小为 54x54 的 UIImageView ,每当我尝试从 JSON 文件下载图像时,它都会显示其原始大小而不是图像视图的大小。

这对于视网膜设备来说是个问题,因为图像必须与图像视图的大小相同。

如何使图像在正确的测量值处调整大小?

http://i.stack.imgur.com/qOQz5.png

这是我所拥有的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UILabel *label;

    label = (UILabel *)[cell viewWithTag:1];
    label.text = [arrayTitle objectAtIndex:indexPath.row];
    label.frame = CGRectMake(70, 8, 209, 21);

    label = (UILabel *)[cell viewWithTag:2];
    label.text = [arraySubtitle objectAtIndex:indexPath.row];
    label.frame = CGRectMake(70, 29, 209, 33);
    label.numberOfLines = 2;

    UIImageView *imgView = (UIImageView *)[cell viewWithTag:0];
    imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [arrayImage objectAtIndex:indexPath.row]]]]];
    imgView.frame = CGRectMake(8, 8, 54, 54);

    cell.imageView.image = imgView.image;

    return cell;
}

【问题讨论】:

    标签: ios json xcode image uiimageview


    【解决方案1】:

    你可以使用这个或尝试减小框架的大小。

    imageView.contentMode = UIViewContentModeScaleAspectFill;
    or
    
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    【讨论】:

    • 这没有帮助,但我已经编辑了包含解决方案的答案。还是谢谢!
    【解决方案2】:

    图像大小是完美的,但您缺少的是几个属性-(以下两个选项中的任何一个选项都可以解决您的问题。)

    1. 内容模式:现在是AspectFill。它应该是 ScaleToFillAspectFit 根据您的要求。

      [imageView setContentMode: UIViewContentModeScaleToFill]; 要么 [imageView setContentMode: UIViewContentModeScaleAspectFill];

    2. 别忘了打开UIImageViewClipSubviews

      [imageView setClipsToBounds:YES];

    【讨论】:

      【解决方案3】:

      解决方案

      我创建了一个 UITableViewCell 类并实现了我的标签和图像视图的所有属性:

      - (void)awakeFromNib {
      
          imageView.frame = CGRectMake(8, 8, 54, 54);
          title.frame = CGRectMake(79, 14, 200, 21);
          subtitle.frame = CGRectMake(79, 31, 200, 33);
          date.frame = CGRectMake((self.window.frame.size.width - 140), 0, 100, 21);
      }
      

      然后我在我的 TableViewController 中调用它:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          static NSString *CellIdentifier = @"Cell";
          HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
      
          cell.title.text = [arrayTitle objectAtIndex:indexPath.row];
          cell.subtitle.text = [arraySubtitle objectAtIndex:indexPath.row];
          cell.date.text = [arrayDate objectAtIndex:indexPath.row];
          cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [arrayImage objectAtIndex:indexPath.row]]]]];
      
          return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-07
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 2021-02-05
        • 2016-06-21
        • 2023-03-06
        • 2014-04-15
        相关资源
        最近更新 更多