iPhone创建自定义图像会导致内存泄漏

时间:2011-12-16 11:48:21

标签: iphone memory-leaks core-graphics

我有一个代码,每次用户移动滑块时都会生成新图像并将其添加到子图层。它的工作原理除了泄漏内存。我保留的是不应该的?但是,我认为我正在释放一切,因为我应该:

UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value);

//add moon mask
UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), NO, 1);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.8);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 0, 0.8);
int tempx = progressAsInt;
int x = tempx; 
if (x < 0) {
    isHigherThanHalf = YES;
    x = -tempx;
}
else {
    isHigherThanHalf = NO;
}

CGContextSaveGState(contextRef);
BOOL onlyDrawTopHalf = isHigherThanHalf;
CGFloat halfMultiplier = onlyDrawTopHalf ? -1.0 : 1.0;
CGRect ellipse = CGRectMake(200-x/2, 0, 0+x, 400);

CGRect clipRect = CGRectOffset(ellipse, halfMultiplier * ellipse.size.width / 2, 0);
CGContextClipToRect(contextRef, clipRect);
CGContextFillEllipseInRect(contextRef, ellipse);
CGContextRestoreGState(contextRef);

CGContextSaveGState(contextRef);
onlyDrawTopHalf = !onlyDrawTopHalf;
halfMultiplier = onlyDrawTopHalf ? -1.0 : 1.0;
ellipse = CGRectMake(0, 0, 400, 400);

clipRect = CGRectOffset(ellipse, halfMultiplier * ellipse.size.width / 2, 0);
CGContextClipToRect(contextRef, clipRect);
CGContextFillEllipseInRect(contextRef, ellipse);
CGContextRestoreGState(contextRef);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
//must free pixels
free(pixels);
pixels = (Byte *)[data bytes];


CGImageRef imageRef = image.CGImage;

// create a new image from the modified pixel data
size_t width                    = CGImageGetWidth(imageRef);
size_t height                   = CGImageGetHeight(imageRef);
size_t bitsPerComponent         = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel             = CGImageGetBitsPerPixel(imageRef);
size_t bytesPerRow              = CGImageGetBytesPerRow(imageRef);

CGColorSpaceRef colorspace      = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo         = CGImageGetBitmapInfo(imageRef);
CGDataProviderRef provider      = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL);

CGImageRef newImageRef = CGImageCreate (
                                        width,
                                        height,
                                        bitsPerComponent,
                                        bitsPerPixel,
                                        bytesPerRow,
                                        colorspace,
                                        bitmapInfo,
                                        provider,
                                        NULL,
                                        false,
                                        kCGRenderingIntentDefault
                                        );
// the modified image
UIImage *newImage   = [UIImage imageWithCGImage:newImageRef];
sublayer2.contents = (id)[newImage CGImage];
[self.view.layer addSublayer:sublayer2];
// cleanup
[data release];
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);
CGImageRelease(newImageRef);

我添加了[数据发布],但似乎仍有一些漏洞。

编辑 - 我发现除了[数据发布]之外,我还需要通过调用来关闭我的图像上下文     UIGraphicsEndImageContext(); 泄漏已经停止。

1 个答案:

答案 0 :(得分:0)

NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));

数据对象未发布。您正在使用自动释放图像对象,有时它会导致内存警告。

UIImage *newImage   = [UIImage imageWithCGImage:newImageRef];

最好手动创建对象,然后在完成对象后释放。因为我们永远不知道自动回收池什么时候会流失。