Search code examples
objective-cuiimageviewcelltableview

Resize tableview's cell image in cellForRowAtIndexPath method


I have a tableview, each cell with a image, title, and subtitle. Each cell has a different image and I can't figure out how to make the right side of them all line up with each other. In other words, some are wider than others and stick further out to the right of the cell, pushing the title and subtitle over as well. I don't care if there are black bars on the right and left, or top and bottom, I just want them to all take up the same amount of space. I tried this is my viewforrowatindexpath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]
                             initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    cell.textLabel.text = [[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] title];
    cell.detailTextLabel.text = (NSString *)[[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] location];
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[mainDelegate.mapAnnotations objectAtIndex:indexPath.row] imageURL]]];
    UIImage *theImage = [[UIImage alloc] initWithData:imageData];

    cell.imageView.frame = CGRectMake(0, 0, 20, 20);
    cell.imageView.image = theImage;
    cell.imageView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:1.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // return it
    return cell;
    [imageData release];
    [theImage release]; 
}

I thought that by setting the frame of the cell's imageview, the image would fit in it but adding that line makes no change when I run the app? Is there a way to accomplish this within the viewforrowatindexpath method?


Solution

  • O.K - I think that is what you are looking for.

    - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
    
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
    
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
    
            if (widthFactor < heightFactor) 
                    scaleFactor = widthFactor;
            else
                    scaleFactor = heightFactor;
    
            scaledWidth  = width * scaleFactor;
            scaledHeight = height * scaleFactor;
    
            // center the image
    
            if (widthFactor < heightFactor) {
                    thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
            } else if (widthFactor > heightFactor) {
                    thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
    }
    
    
    // this is actually the interesting part:
    
    UIGraphicsBeginImageContext(targetSize);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if(newImage == nil) NSLog(@"could not scale image");
    
    
    return newImage ;
    }
    

    I found it here - how to scale a uiimageview proportionally