Set Image size in cell
UITableView provides default functionality for adding an image in the cell but it will take the size of the image according to their row size and difficult to adjust their size according to our need . if we are taking customize UITableView then it is easy but if we are using their default feature to add an image then it will create a problem.
Steps you have to follow:
1: Delegate function of UITable where you are adding an image in the cell, write this code…
This is the code snippet. the actual function is. ex:-( 40 x 40)
1 |
[self imageWithImage:imageIcon scaledToSize:CGSizeMake(40,40)]; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.section == 0){ cell.imageView.image = [UIImage imageNamed:@"ic_placeholder.png"]; for(int i = 0; i < [categoryImagesId count]; i++){ if([[categoryImagesId objectAtIndex:i] isEqualToString:[categoryId objectAtIndex:[indexPath row]]]){ UIImage *imageIcon = [imageCache objectForKey:[categoryImages objectAtIndex:[indexPath row]]]; if(imageIcon !=nil){ cell.imageView.image = [self imageWithImage:imageIcon scaledToSize:CGSizeMake(40,40)]; } else{ GlobalData *obj = [GlobalData getInstance]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[categoryImages objectAtIndex:[indexPath row]]]]]; if(image!=nil) [obj.gloablCatalogIconCache setObject:image forKey:[categoryImages objectAtIndex:[indexPath row]]]; //cell.imageView.image = image; } break; } } } else cell.imageView.image = [UIImage imageNamed:@""]; cell.textLabel.text = contentForThisRow; if([[languageCode stringForKey:@"language" ] isEqualToString:@"ar"]) cell.textLabel.textAlignment = NSTextAlignmentRight; return cell; } |
2: Define that function in same class.
1 2 3 4 5 6 7 |
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } |
3: Now you can manage your image according to need like 30×30,20×20 …. etc.