Iphone 限制以编程方式设置的UIImage的大小

Iphone 限制以编程方式设置的UIImage的大小,iphone,cocoa-touch,ios5,Iphone,Cocoa Touch,Ios5,我正在从web服务动态加载图像。图像将被加载(带有[UIImage imageWithData:data];),并使用setImage消息放置在我的自定义UITableViewCell中的UIImageView中 。我在interface builder中给imageView设置了27 x 19的宽度和高度,并告诉他“Aspect fit”(顺便说一句,我也尝试了所有其他方法),但图像没有做到这一点。它只是缩放(在方面,我认为)以填充视图单元格 -(UITableViewCell *) tabl

我正在从web服务动态加载图像。图像将被加载(带有
[UIImage imageWithData:data];
),并使用
setImage
消息放置在我的自定义
UITableViewCell
中的
UIImageView

。我在interface builder中给imageView设置了27 x 19的宽度和高度,并告诉他“Aspect fit”(顺便说一句,我也尝试了所有其他方法),但图像没有做到这一点。它只是缩放(在方面,我认为)以填充视图单元格

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       static NSString *SimpleTableIdentifier = @" SimpleTableIdentifier ";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   SimpleTableIdentifier];
       if (cell == nil) 
       { 
          cell = [[[UITableViewCell alloc]
          initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
       }

      UIImage *image = [UIImage imageNamed:@"star.png"]; 
      cell.imageView.image = image;
      NSUInteger row = [indexPath row]; 
      cell.textLabel.text = [listData objectAtIndex:row];
      return cell;
}

//use this delegate it will increase the row size and thus your imagesize will get increased too.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 80;   //value can be changed
}
我尝试了很多事情,比如调整图像的大小(做这项工作,但我可以计算视网膜显示器上的像素),调整UIImageView的大小。。。但我就是不明白。。。有人知道我做错了什么

更改单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
        cell = self.tableCell;
        self.tableCell = nil;
    }

    viArticle *article = [self.viData.articles objectAtIndex:indexPath.row];

    UILabel *label;

    label = (UILabel *)[cell viewWithTag:1];
    label.text = article.title;

    label = (UILabel *)[cell viewWithTag:2];
    label.text = [NSString stringWithFormat:@"%@ %@", article.time, article.type];

    if (article.image != nil) {
        UIImageView *imageView;
        imageView = (UIImageView *)[cell viewWithTag:0];
        [imageView setImage:article.image];

    }

    return cell;
}
这是一种观点:

以及设置:


如果要调整
UIImageView
的大小,可以使用frame属性

假设您有一个
UIImageView
在.h文件中声明(让我们使用
UIImageView*myImage;
)并且它也链接到界面生成器。 你可以在.m类中合成它

然后,如果要调整UIImageView的大小,请使用

myImage.frame = CGRect(x,y,width,height);
注意:通过在代码中声明宽度和高度,IB中声明的宽度和高度将被覆盖

然后别忘了将其添加到子视图

i.e [self.view addSubview:myImage];

完成后,您可以看到更改。

如果取消勾选“自动调整子视图大小”,会发生什么情况?

@GuidoH嘿,很抱歉回复太晚。。 下面是将图像添加到表视图单元格的代码

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       static NSString *SimpleTableIdentifier = @" SimpleTableIdentifier ";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   SimpleTableIdentifier];
       if (cell == nil) 
       { 
          cell = [[[UITableViewCell alloc]
          initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
       }

      UIImage *image = [UIImage imageNamed:@"star.png"]; 
      cell.imageView.image = image;
      NSUInteger row = [indexPath row]; 
      cell.textLabel.text = [listData objectAtIndex:row];
      return cell;
}

//use this delegate it will increase the row size and thus your imagesize will get increased too.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 80;   //value can be changed
}
我建议缩放图像肯定会导致应用程序的性能降低。

我今天早些时候刚刚发布了一个“解决方案”,但这似乎可行,但这是一个错误的假设。在询问IRC后,我找到了真正的解决方案。我使用的是
标记
0,不应该使用该标记。将其更改为更高的值确实解决了我的问题。有时这很容易


谢谢大家

你这里的问题还不清楚。您已指示它使用“Aspect fit”,并且正在缩放图像以填充视图。。。这正是你要求它做的。也许您的目标是错误的视图?它应该在
UIImageView
内部“方面适合”,但它在外部适合。。我不知道为什么。如果我在IB中添加图像,它会正确显示。我想我们需要查看您使用的代码。我刚刚编辑了我的问题。:)这是我尝试过的事情之一,它似乎没有改变任何事情。但我为什么要将其添加到子视图?它是内置在IB中的,所以它已经存在了?当您为它定义一个新的框架时,您可以将它添加到子视图中。@GuidoH您可以尝试它,因为它对我有用。如果它也适用于你,那么请将它标记为答案。不会改变任何事情。当我看到这里的图片时,我试过了。谢谢你的回答。但这并不能真正解决我的问题。我可以将图像添加到单元格中,但显示的大小不正确。我从一个URL获取图像,该URL返回图像。但是,当我将其添加到图像视图时,它不会正确显示,即使我将图像服务器端的大小调整为正确的大小。我想问题可能在于我在视网膜显示器上使用它,而IB不使用那个尺寸。