2009-10-01 8 views
0

J'utilise un contrôleur de navigation dans lequel je pousse un contrôleur tableview comme suit:Comment recadrer ou obtenir une UIImage de plus petite taille dans l'iPhone sans fuites de mémoire?

TableView * Controller = [[TableView alloc] initWithStyle: UITableViewStylePlain]; [self.navigationController pushViewController: Contrôleur animé: NON]; [Version du contrôleur];

Dans ce point de vue de la table J'utilise deux méthodes suivantes pour afficher des images:

- (UIImage*) getSmallImage:(UIImage*) img 
{ 
    CGSize size = img.size; 
    CGFloat ratio = 0; 
    if (size.width < size.height) { 
     ratio = 36/size.width; 
    } else { 
     ratio = 36/size.height; 
    } 
    CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height); 

    UIGraphicsBeginImageContext(rect.size); 
    [img drawInRect:rect]; 

    return UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect 
{ 

    //create a context to do our clipping in 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //create a rect with the size we want to crop the image to 
    //the X and Y here are zero so we start at the beginning of our 
    //newly created context 

    CGFloat X = (imageToCrop.size.width - rect.size.width)/2; 
    CGFloat Y = (imageToCrop.size.height - rect.size.height)/2; 


    CGRect clippedRect = CGRectMake(X, Y, rect.size.width, rect.size.height); 
    //CGContextClipToRect(currentContext, clippedRect); 



    //create a rect equivalent to the full size of the image 
    //offset the rect by the X and Y we want to start the crop 
    //from in order to cut off anything before them 
    CGRect drawRect = CGRectMake(0, 
           0, 
           imageToCrop.size.width, 
           imageToCrop.size.height); 

    CGContextTranslateCTM(currentContext, 0.0, drawRect.size.height); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    //draw the image to our clipped context using our offset rect 
    //CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage); 


    CGImageRef tmp = CGImageCreateWithImageInRect(imageToCrop.CGImage, clippedRect); 

    //pull the image from our cropped context 
    UIImage *cropped = [UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext(); 
    CGImageRelease(tmp); 
    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    //Note: this is autoreleased*/ 
    return cropped; 
} 

Mais quand je pop le contrôleur, la mémoire utilisée est pas libéré. Y at-il des fuites dans le code ci-dessus utilisé pour créer et recadrer les images.

Existe-t-il une méthode efficace pour gérer les images sur iPhone? J'ai beaucoup d'images et je suis confrontée à des difficultés majeures pour résoudre les problèmes de mémoire.

tnx à l'avance.

Répondre

1

Je ne sais pas à ce sujet, mais cela pourrait être un problème:

return UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

la deuxième ligne est jamais exécuté, parce que le premier retourne

Questions connexes