2010-07-08 4 views
0

Je reçois des images de flux XML, ils sont de très grande taille comment puis-je les compresser avant de les afficher dans la table-cellule.comment compresser l'image par programme

mon code est

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]; 

s'il vous plaît aidez-moi si vous pouvez .....

+0

Il serait préférable de les compresser à la source - Au moment où vous l'avez envoyé au téléphone, vous avez payé pour le trafic. –

+0

Les images proviennent du site Web via le flux Rss. –

Répondre

1

J'ai cette méthode utilitaire qui échelle d'une 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; 
} 

Utilisation comme ceci:

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 Je ne peux pas pour la vie de me rappeler où je l'ai eu de mais il m'a bien servi dans le passé

+0

VEUILLEZ NOUS ME DIRE COMMENT APPELER CETTE MÉTHODE CELLULAIRE –

+0

J'appelle la méthode ci-dessus comme ceci .... CGSize itemSize = CGSizeMake (20,40); \t \t .... [self imageWithImage: img scaledToSize: itemSize]; –

+0

mais ne fonctionne pas –

0

Simple à utiliser: -

-(UIImage *)fireYourImageForCompression:(UIImage *)imgComing{ 

NSData *dataImgBefore = [[NSData alloc] initWithData:UIImageJPEGRepresentation((imgComing), 1.0)];//.1 BEFORE COMPRESSION 
int imageSizeBefore  = (int)dataImgBefore.length; 


NSLog(@"SIZE OF IMAGE: %i ", imageSizeBefore); 
NSLog(@"SIZE OF IMAGE in Kb: %i ", imageSizeBefore/1024); 



NSData *dataCompressedImage = UIImageJPEGRepresentation(imgComing, .1); //.1 is low quality 
int sizeCompressedImage  = (int)dataCompressedImage.length; 
NSLog(@"SIZE AFTER COMPRESSION OF IMAGE: %i ", sizeCompressedImage); 
NSLog(@"SIZE AFTER COMPRESSION OF IMAGE in Kb: %i ", sizeCompressedImage/1024); //AFTER 

//now change your image from compressed data 
imgComing = [UIImage imageWithData:dataCompressedImage]; 


return imgComing;}