views:

6267

answers:

5

I use the same big images in a tableView and detailView. Need to make imageView filled in 40x40 when an imags is showed in tableView, but stretched on a half of a screen. I played with several properties but have no positive result:

    [cell.imageView setBounds:CGRectMake(0, 0, 50, 50)];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];

I am using SDK 3.0 with build in "Cell Objects in Predefined Styles".

A: 

you might be able to use this?

yourTableViewController.rowImage = [UIImage imageNamed:@"yourImage.png"];

and/or

cell.image = yourTableViewController.rowImage;

and if your images are already 40x40 then you shouldn't have to worry about setting bounds and stuff... but, i'm also new to this, so, i wouldn't know, haven't played around with Table View row/cell images much

hope this helps.

Wolfcow
No, it is not that i need. I need big images to use.
slatvick
He means changing the size, although you are correct in setting the image.
JoePasq
+1  A: 

I was able to make this work using interface builder and a tableviewcell. You can set the "Mode" properties for an image view to "Aspect Fit". I'm not sure how to do this programatically.

Jonah
This is how you would do it programmatically:yourTableViewCell.imageView.contentMode = UIViewContentModeScaleAspectFit;
Jonah
A: 

Hi slatvick

Have you tried setting UIImageView.autoresizesSubviews and/or UIImageView.contentStretch?

A: 

I have mine wrapped in a UIView and use this code:

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
    [self addSubview:imageView];
    imageView.frame = self.bounds;

(self is the wrapper UIView, with the dimensions I want - I use AsyncImageView).

alex_c
A: 

I cache a thumbnail version since using large images scaled down on the fly uses too much memory.

Here's my thumbnail code:

- (UIImage *)thumbnailOfSize:(CGSize)size {
    if( self.previewThumbnail )
     return self.previewThumbnail; // returned cached thumbnail

    UIGraphicsBeginImageContext(size);

    // draw scaled image into thumbnail context
    [self.preview drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext(); 

    // pop the context
    UIGraphicsEndImageContext();

    if(newThumbnail == nil) 
        NSLog(@"could not scale image");

    self.previewThumbnail = newThumbnail;

    return self.previewThumbnail;
}

Just make sure you properly clear the cached thumbnail if you change your original image (self.preview in my case).

Ben Lachman