views:

66

answers:

1

Hi all, I am receiving images from xml feed they are very large in size how can i compress them before displaying them in table-cell.

my code is

   int blogEntryIndex1 = [indexPath indexAtPosition: [indexPath length] -1];
    imgstring=[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];
NSURL *url = [NSURL URLWithString:imgstring];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image=[img autorelease];

please help me if you can.....

+1  A: 

Hi,

I've got this utility method that will scale an image :

- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

Use it like this :

int blogEntryIndex1 = [indexPath indexAtPosition: [indexPath length] -1];
imgstring=[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [self imageWithImage:[UIImage imageWithData:data] scaledToSize:CGSizeMake(20, 20)]; // Scale it to 20x20px
cell.imageView.image=[img autorelease];

NB I can't for the life of me remember where I got it from but it's served me well in the past

deanWombourne
PLEASE TELL ME HOW TO CALL THIS FROM CELL METHOD
Nauman.Khattak
i am calling the above method like this....CGSize itemSize = CGSizeMake(20,40); ....[self imageWithImage:img scaledToSize:itemSize];
Nauman.Khattak
but its not working
Nauman.Khattak
See my edit above - it doesn't change the existing image, it creates a new, smaller one.
deanWombourne
THANKS DEAN YOU ARE GREAT........ALWAYS HELPFULL AND SPOT ON...
Nauman.Khattak
But there is still the same problem of lazy image loading how should i get rid of this my application hang down when i scroll cells
Nauman.Khattak