Sto usando il codice seguente in iPhone a diminuire immagine ritagliata come segue:allocazione di memoria e rilascio per UIImage in iPhone?
- (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];
UIImage *tempImg = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
return [tempImg autorelease];
}
- (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;
}
Sto usando seguente riga di codice in cellForRowAtIndexPath per aggiornare l'immagine della cella:
cell.img.image = [self imageByCropping:[self getSmallImage:[UIImage imageNamed:@"goal_image.png"]] toRect:CGRectMake(0, 0, 36, 36)];
Ora, quando Aggiungo questa vista tabella e la apro dal controller di navigazione, vedo un aumento della memoria. Non vedo perdite ma la memoria continua a salire.
Si noti che le immagini cambiano per ogni riga e sto creando il controller utilizzando l'inizializzazione pigra che creo o lo assegno ogni volta che ne ho bisogno.
Ho visto su Internet molte persone che affrontano lo stesso problema, ma soluzioni molto rare. Ho più viste che usano allo stesso modo e vedo quasi memoria aumentata a 4 MB entro 20-25 transizioni di visualizzazione.
Qual è la soluzione migliore per risolvere questo problema.
tnx.
Ho provato, ma ancora non risolve il problema. Vedo ancora gli aumenti della memoria. – rkb