下载的图像比想要的大

时间:2014-12-29 04:38:41

标签: ios json xcode image uiimageview

问题

我正在开发一个应用程序,我从一个网站上传的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;
}

3 个答案:

答案 0 :(得分:2)

<强>解

我创建了一个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;
}

答案 1 :(得分:1)

你可以使用它或尝试减小帧的大小。

imageView.contentMode = UIViewContentModeScaleAspectFill;
or

imageView.contentMode = UIViewContentModeScaleAspectFit;

答案 2 :(得分:0)

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

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

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

  2. 请勿忘记启用UIImageView ClipSubviews

    [imageView setClipsToBounds:YES];

相关问题