Limit the size of an UIImage which is set programmatically

I'm dynamically loading images from a web service. The images will be loaded (with [UIImage imageWithData:data];) and be placed in a in an UIImageView in my custom UITableViewCell using the setImage message

. I gave the imageView a width and height of 27 by 19 in the interface builder and told him to "Aspect fit" (tried all the others too btw), but the image doesn't do that. It just scales (in aspect, I think) to fill the view cell.

I tried a lot of things, like resizing the image (does the job, but I can count the pixels on my retina display), resizing the UIImageView... But I just don't get is... Someone got an idea what I'm doing wrong?

The code that changes the cell:

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

This is the view:

And the settings:

Answers


If you want to resize the UiImageview you can use the frame property.

suppose you have a UIImageView declare in .h file(lets take UIImageView *myImage) and it is linked to the interface builder too. you have synthesis it in .m class etc.

then when you want to resize the UIImageView, use

myImage.frame=CGRect(x,y,width,height);

Note: by declaring the width and height in the code, the width and height declared in the IB will be override.

then dont forget to add it to subview, i.e [self.view addSubview:myImage];

and its done, you can see the changes.


What happens if you untick "Autoresize Subviews"?


@GuidoH hey sorry for the late reply.. Here is the code that will add the image to the table view cell.

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

I would suggest scaling the images will surely lead to slow performance by your app.


I just posted a "solution" earlier today, but that seem to work, but was a wrong assumption. After asking on IRC I found the real solution. I was using the tag 0, that one shouldn't be used. Changing it to an higher value really solved my problem. Sometimes it can be thát easy. :)

Thanks y'all.


Need Your Help

Create a hierarchical dynamic grid with GWT

gwt grid hierarchical-data

I'm trying to create a grid in GWT representing hierarchical data.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.