1

I have URL from a JSON string. I get image for JSON. and then display on UITableViewCell. However my problem is every image not same height and width.

I pass the image array like this.

cell.imageView.image = [imagesarray objectAtIndex:indexPath.row];

For example, I have get 3 images first image size 200*200 and second image size 80*80 and thread images size 120*120.

My questions are:

  • How do I fix every image to be size 80*80
  • How to reduces UITableViewCell image height and width
Rapptz
  • 20,807
  • 5
  • 72
  • 86
ios start
  • 65
  • 8

3 Answers3

1

If you want to have fixed width - divide image width by imageView width and name it "ratio". Then divide imageHeight by ratio and you will get needed height of imageView. Then set new frame. How to do that might depend on your design (you might want to change origin, or leave it the same).

For fixed height it is similar.

To set size of your imageView use

[cell.imageView setFrame:CGRectMake(newX, newY, newWidth, newHeight)];

For resizing of Images and not imageView, you can find help there: The simplest way to resize an UIImage?

Community
  • 1
  • 1
Reconquistador
  • 885
  • 8
  • 28
  • 1
    he wants to scale the image as I understand it – Daij-Djan Aug 22 '14 at 09:52
  • My comment is about scaling imageView, which will change size of image on the screen. But now, after new edits, he probably even wants to change size of every UIImage, which has not been clear in the time of my answer. – Reconquistador Aug 22 '14 at 09:54
0

1 scale the image

a nice category:

header

#import <UIKit/UIKit.h>

@interface UIImage (Scale)

- (UIImage*)imageScaledToSize:(CGSize)size alpha:(CGFloat)alpha;

- (UIImage*)imageScaledToSize:(CGSize)size;
- (UIImage*)imageScaledToFit:(CGSize)size;
- (UIImage*)imageScaledToFill:(CGSize)size;

@end

implementation

@implementation UIImage (Scale)

- (UIImage*)imageScaledToSize:(CGSize)size alpha:(CGFloat)alpha {

    // Create a bitmap graphics context
    // This will also set it as the current context
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); //respect to Retina display

    // Draw the scaled image in the current context
    [self drawInRect:CGRectMake(0, 0, (int)size.width, (int)size.height)
           blendMode:kCGBlendModeMultiply
               alpha:alpha];

    // Create a new image from current context
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    // Pop the current context from the stack
    UIGraphicsEndImageContext();

    // Return our new scaled image
    return scaledImage;
}

- (UIImage*)imageScaledToSize:(CGSize)size {
    return [self imageScaledToSize:size alpha:1];
}

- (UIImage*)imageScaledToFit:(CGSize)size {
    CGFloat scale = size.width / self.size.width;
    scale = MIN(scale, size.height / self.size.height);

    return [self imageScaledToSize:CGSizeMake(self.size.width * scale, self.size.height * scale)];
}

//TODO: crop frame
- (UIImage*)imageScaledToFill:(CGSize)size {
    CGFloat scale = size.width / self.size.width;
    scale = MAX(scale, size.height / self.size.height);

    return [self imageScaledToSize:CGSizeMake(self.size.width * scale, self.size.height * scale)];
}

@end

usage

UIImage *img = [imagesarray objectAtIndex:indexPath.row];
cell.imageView.image =[img imageScaledToFit:CGSizeMake(80, 80)];

2 set size of tableView cell imageView

[cell.imageView setFrame:CGRectMake(newX, newY, newWidth, newHeight)];
//E.G. [cell.imageView setFrame:CGRectMake(0, 0, 80, 80)];
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
-1

Try with this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{         
    UIImage *image=[imagesarray objectAtIndex:indexPath.row];
    CGRect imageRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = [imagesarray objectAtIndex:indexPath.row];
}

and

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{                 
      UIImage *image=[imagesarray objectAtIndex:indexPath.row];        
      return image.size.height;
 }
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
Vijay
  • 174
  • 2
  • 9
  • drawInRect? the imageView shouldn't drawInRect at that point .. but even besides this: this don't work at all – Daij-Djan Aug 22 '14 at 10:04