UIImage утечка памяти - PullRequest
       14

UIImage утечка памяти

0 голосов
/ 17 сентября 2011

Может ли кто-нибудь разобраться с моей проблемой утечки памяти в следующем коде ???

-(void)paint:(ImageWarper::WarpedImage *)warpedImg isCircleRequired:(bool)doDrawCircle atPoint:(CGPoint)pt{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(!mWarper) 
    return;
unsigned char *pixelData = warpedImg->Image.Data;
int imageHeight  = warpedImg->Image.Height;
int scanWidth = warpedImg->Image.ScanWidth;
int imageWidth = warpedImg->Image.Width;
CGDataProviderRef provider = CGDataProviderCreateWithData(
                                                          NULL, 
                                                          pixelData, 
                                                          imageHeight * scanWidth, 
                                                          NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
int bytesPerPixel = warpedImg->Image.Bpp;
CGImageRef imageRef = CGImageCreate(imageWidth,
                                    imageHeight,
                                    BitsPerComponent,
                                    bytesPerPixel * BitsPerComponent,
                                    scanWidth,
                                    colorSpaceRef,
                                    bitmapInfo,
                                    provider,
                                    NULL,
                                    YES,
                                    renderingIntent);

UIImage *uiImage = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
//  CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);
imgScrollView.imgView.image = uiImage;
UIGraphicsBeginImageContext(mbmpImage.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 1.5);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);

[mbmpImage drawInRect:CGRectMake(0, 0, mbmpImage.size.width, mbmpImage.size.height)]; 
[uiImage drawInRect:CGRectMake(warpedImg->Position.X, warpedImg->Position.Y, warpedImg->Image.Width, warpedImg->Image.Height)];         
[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
if(doDrawCircle){
    mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];           
    CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
}
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    

UIGraphicsEndImageContext();
imgScrollView.imgView.image =  resultingImage ;
if(!doDrawCircle)       
    mbmpImage = [resultingImage retain];

[resultingImage release];
[pool drain];
}

Эта функция вызывается при сенсорных событиях.

Это инструмент, на котором видна утечка ..

1 Ответ

2 голосов
/ 17 сентября 2011

Заменить

[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
if(doDrawCircle){
    mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];           
    CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
}
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    

UIGraphicsEndImageContext();
imgScrollView.imgView.image =  resultingImage ;
if(!doDrawCircle)       
    mbmpImage = [resultingImage retain];

[resultingImage release];
[pool drain];

с

[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
if(doDrawCircle)
    CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    

UIGraphicsEndImageContext();
imgScrollView.imgView.image =  resultingImage ;
if(!doDrawCircle)
{
    [mbmpImage release];
    mbmpImage = [resultingImage retain];
}

[resultingImage release];
[pool drain];
...