0

resize a image in table view cell... i am new programmer

by this code image comes in cell... but i wants resize image according to my need....

if possible give me little code... thanks in advance...

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {
        //.......
    }
    search_items *pro = [result objectAtIndex:0];

    if(indexPath.row==0)
    {

    NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:@"png"];

    UIImage *image=[UIImage imageWithContentsOfFile:filepath];

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

    //[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];

    [cell.imageView setFrame:CGRectMake(50,50,100,100)];

    /*
    CGRect iframe = cell.imageView.frame;
    iframe.size.height = 100.0;
    iframe.size.width=300.0;

    cell.imageView.frame = iframe;

    */


    cell.imageView.image=image; 
    }
}
GauravBoss
  • 140
  • 2
  • 4
  • 13

4 Answers4

3

Rather than resizing the cell.imageView frame, try resizing the image before assigning it to cell.imageView.image:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = @"My Cell's Title Text";
    cell.detailTextLabel.text = @"My Cell's Description Text";

    //### SETTING THE IMAGE HERE###
    //Let's say I want to resize my image entitled "myImage.png" to be 50x50 pixels in the table cell
    CGSize size = {50,50};
    cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"myImage.png" scaledToSize:size];

    return cell;
}

//Given a UIImage and a CGSize, this method will return a resized UIImage.
- (UIImage*)imageWithImage:(UIImage*)image
          scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

Sources:

Community
  • 1
  • 1
MFarmer
  • 1,686
  • 13
  • 9
2

You can change the frame of the imageView like this:

CGRect frame = imageView.frame;
frame.size.height = 30.0;
/* other frame changes ... */
imageView.frame = frame;

You should also look at the UIImageView contentMode property (see this post). It gives you control over how the image is drawn, and is actually a UIView property (so UIImageView inherits it). It gives you the following options:

Specifies how a view adjusts its content when its size changes.

typedef enum {
   UIViewContentModeScaleToFill,
   UIViewContentModeScaleAspectFit,
   UIViewContentModeScaleAspectFill,
   UIViewContentModeRedraw,
   UIViewContentModeCenter,
   UIViewContentModeTop,
   UIViewContentModeBottom,
   UIViewContentModeLeft,
   UIViewContentModeRight,
   UIViewContentModeTopLeft,
   UIViewContentModeTopRight,
   UIViewContentModeBottomLeft,
   UIViewContentModeBottomRight,
} UIViewContentMode;
Community
  • 1
  • 1
jbat100
  • 16,757
  • 4
  • 45
  • 70
  • thanks..for giving your time...... but no any effect on image after this code....edited above – GauravBoss Nov 06 '11 at 10:23
  • try adding cell.imageView.contentMode = UIViewContentModeScaleAspectFill; – jbat100 Nov 06 '11 at 10:35
  • @jbat.. i try it... it works ..but i wants place image in desired location ..like i wants frame ...[cell.imageView setFrame:CGRectMake(50,50,100,100)]... no errors...but no effect – GauravBoss Nov 06 '11 at 11:16
  • When you do [cell.imageView setFrame:CGRectMake(50,50,100,100)]; you are setting a y origin of 50, which means if your cell is less than 50 high, you won't see anything at all (it will be off the cell bounds), you can try [cell.imageView setFrame:CGRectMake(50,0,100,100)]; to just change the x origin. – jbat100 Nov 06 '11 at 11:21
  • check what the superview of imageView is (the frame origin is with reference to the superview) – jbat100 Nov 06 '11 at 11:29
  • @jbat...thanks. you are great Man... i take your so much time... thanks again – GauravBoss Nov 06 '11 at 11:39
  • just post specific questions up to this site :) – jbat100 Nov 06 '11 at 12:15
1

You should use the backgroundView property of your cell and add a stretchable image :

UIImage* backgroundImage = [[UIImage imageNamed:@”stretchableImage.png”] stretchableImageWithLeftCapWidth:44 topCapHeight:45];

Hope this helps, Vincent

vdaubry
  • 11,369
  • 7
  • 54
  • 76
0

1°) Well, by resizing, you mean which is scaled properly? You should check the contentMode property of the UIImageView class [ cell.imageView setContentMode: ];

2°) If you want to resize the frame, then just do [ cell.imageView setFrame:CGRectMake() ];

Depends what's really your problem ^^

Good Luck.

Vinzius
  • 2,890
  • 2
  • 26
  • 27
  • @vinzius... i wants place image in desired location ..like i wants frame ...cell.imageView setFrame:CGRectMake(50,50,100,100)... no errors...but no effect – GauravBoss Nov 06 '11 at 11:18