Snehal
Snehal

Reputation: 154

Query regarding "CGImageCreateWithImageInRect"

im trying to cut a image and mask it....that im able to do successfully..but the program exits after few minutes with 101 status

- (void) maskImage {

        if(scopeOn==1){

    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    cachedImage=[UIImage imageNamed:@"loop.png"];
    cachedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

     imageRef = [cachedImage CGImage];

     subImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(scopeLoc.x-25, scopeLoc.y-25, 50, 50));
     xMaskedImage = CGImageCreateWithMask(subImage, mask);
    zoomImg.image = [UIImage imageWithCGImage:xMaskedImage]; // retImage;
    [zoomImg setCenter:scopeLoc];

    [self addSubview:zoomImg];

    CGImageRelease(subImage);
            CGImageRelease(xMaskedImage);

}

}

this is the code that im using....since im not allocating explicit memory my guess is that CGImageCreateWithImageInRect function is allocating memory but it is not being released...this function is called after every 0.1 secs...so eventually a large amount of memoey is allocated(i have seen this in memory leak performance monitor)

So is there any other way in which i can achive the same wihtout this function??

Upvotes: 1

Views: 2571

Answers (3)

Jyotsna
Jyotsna

Reputation: 4441

You can release the memory allocated for your subImage and xMaskedImage by following

    CGImageRelease(subImage);           // Decrements the retain count of a bitmap image.
    subImage=nil;
CGImageRelease(xMaskedImage);
xMaskedImage=nil;

It will definitely solved your problem.

Upvotes: 1

cobbal
cobbal

Reputation: 70765

Maybe try adding a test for if zoomImg is already a subview:

if (zoomImg.superview != self)
    [self addSubview:zoomImg];

although this is a fairly long shot.

Upvotes: 0

Ecton
Ecton

Reputation: 10722

Are you releasing the subImage variable later? CGImageCreateWithImageInRect follows the "Create" rule from CoreFoundation, and thus requires you to release the variable later.

Upvotes: 4

Related Questions