UIImage内存泄漏

时间:2011-09-17 06:32:55

标签: iphone memory-leaks uiimage

任何人都可以在下面的代码中解决我的内存泄漏问题吗?

-(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 个答案:

答案 0 :(得分:2)

替换

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