I have a code that generates new image every time user moves slider a little and adds it to sublayer. It works except it leaks memory. What I am retaining that i shouldn't? However i think i am releasing everything as i should:
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);
I have added [data release] but there still seems to be some leaking.
EDIT - I have found that in addition to [data release] i need to close my image context by calling UIGraphicsEndImageContext(); The leaks have stopped.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)