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;
}
}
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:
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;
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.