-2

I add the picture in my UITableView by JSON (just pass the URL) and now I want to adjust size of images, I want to adjust them in same size in the main page like thumbnail. thank you.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    City * cityObject;
    cityObject = [ citiesArry objectAtIndex:indexPath.row];
    cell.textLabel.text = cityObject.cityState;
    cell.detailTextLabel.text = cityObject.cityName;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.numberOfLines = 4;
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cityObject.cityImage]  ];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]  ];
    return cell;
}
Buntylm
  • 7,345
  • 1
  • 31
  • 51
Ali
  • 29
  • 5
  • 3
    so what's your problem? – Andrey Chernukha Mar 11 '14 at 05:27
  • I want to have all the pictures in one size. now I got all pictures in different size. – Ali Mar 11 '14 at 05:30
  • did you google RESIZE UIIMAGE? – Andrey Chernukha Mar 11 '14 at 05:32
  • the code you've adduced is not relevant. all you need is to resize all the images to be of one certain size. that's it. with the google search i've just told you about you'll find it easily – Andrey Chernukha Mar 11 '14 at 05:34
  • simply you can say I dont know or do not comment when you dont know anything ! – Ali Mar 11 '14 at 05:39
  • 1
    well, buddy, i KNOW. but what's wrong with what i've just said? This is what you should do on SO. Google first instead of posting irrelevant code. Ask anyone if you don't believe me – Andrey Chernukha Mar 11 '14 at 05:41
  • look here http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage . i found this answer within a second. are you going to tell me you were not able to do it yourself? – Andrey Chernukha Mar 11 '14 at 05:44
  • one more http://stackoverflow.com/questions/2645768/uiimage-resize-scale-proportion – Andrey Chernukha Mar 11 '14 at 05:45
  • 1
    you're crazy, have you ever been told that? that question of mine got 7 upvotes, how many do you think this one will get? i can post my comments wherever i want to, without asking anyone. You're here just for few weeks and you start going mad after being told in a polite way how you should act here. Are you sure you're right? If you have already performed google search so why do you ask your question? why do you post this code? what kind of relation do you see between this code and your issue? – Andrey Chernukha Mar 11 '14 at 05:56
  • 1
    i know EVERYTHING about your problem, your question is absolutely simple, why on earth did you decide that i don't know the answer? Saying "did you google it?" is absolutely correct here, and what you're saying is not. i wasn't mocking you in any way. I told you what to do, i gave you the key to your problem. You think i'm not right? Let's ask people. Let people decide who of us is right or wrong. – Andrey Chernukha Mar 11 '14 at 06:11
  • 1
    Ali, why do you think that i judge you in any way? what made you think so? If you knew me in person you would be aware of the fact that i never judge people. You want my answer? Wait a second. But i gave you two links. There are a lot of examples of resizing an UIImage there. What is still confusing you? – Andrey Chernukha Mar 11 '14 at 06:28
  • It is not good to change the size of the image w.r.t. their size constraints. Better to change their aspect ratio. – Himanshu Joshi Mar 11 '14 at 06:30
  • @HimanshuJoshi could you please explain it more for me or any sample code? – Ali Mar 11 '14 at 06:57
  • You can use `contentMode` property of UIImageView `imageView.contentMode = UIViewContentModeScaleAspectFit;`. – Himanshu Joshi Mar 11 '14 at 06:59

3 Answers3

1

On the top of your .m file:

#define MySize CGSizeMake(100, 100) // any size that you need

Then you make a category method on UIImage or wherever you like:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;
}

Then your cellForRowAtIndexPath looks like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
City * cityObject;
cityObject = [ citiesArry objectAtIndex:indexPath.row];
cell.textLabel.text = cityObject.cityState;
cell.detailTextLabel.text = cityObject.cityName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.numberOfLines = 4;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cityObject.cityImage]  ];
UIImage *image = [UIImage imageWithData:imageData];
image = [UIImage imageWithImage:image scaledToSize:MySize];
[[cell imageView] setImage:image];
return cell;
}

that's it except that it's strongly recommended not to use [NSData dataWithContentsOfURL: ]; on the main thread. You should get your data in advance on a secondary thread

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • Thanks man after long discussion together ;) let me try that first. thanks for timing and your answer. – Ali Mar 11 '14 at 06:56
0

you can use the following:

 + (UIImage *)scaleImage:(NSData *)imageData toSize:(CGSize)size {
    UIImage *image = [UIImage imageWithData:imageData];
    CGFloat scale = [[UIScreen mainScreen]scale];
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    [image drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

I've just subclassed UIImage and added it as a class method. Hope it helps

HorseT
  • 6,592
  • 2
  • 18
  • 16
0

If you want to scale the image you can use CoreGraphics functions

- (UIImage*)scaleImage:(UIImage*)image toSize:(CGSize)newSize {
    CGSize scaledSize = newSize;
    float scaleFactor = 1.0;
    if( image.size.width > image.size.height ) {
        scaleFactor = image.size.width / image.size.height;
        scaledSize.width = newSize.width;
        scaledSize.height = newSize.height / scaleFactor;
    }
    else {
        scaleFactor = image.size.height / image.size.width;
        scaledSize.height = newSize.height;
        scaledSize.width = newSize.width / scaleFactor;
    }

    UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
    CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
    [image drawInRect:scaledImageRect];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return scaledImage;
}
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32