how to release data generated by UIImageJPEGRepresentation or UIImagePNGRepresentation?

3.6k views Asked by At

I have a problem like that :

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSData *data;

NSString *file1 = [[NSBundle mainBundle] pathForResource:
    [NSStringstringWithFormat:@"originimg_%d.jpg",i] ofType:nil]] ;

UIImage *image1 = [[UIImage alloc]initWithContentsOfFile:file1];
data = UIImageJPEGRepresentation(image, 0.7);
// do sth with data ...

[image1 release];
image1 = nil;
[pool drain];   
pool = nil;
if(data)
    NSLog(@"still exist");

I checked whether data still exist in memory, (I expected it is removed after i drain the autorelease pool) but it still existed :(. Do you have any idea how to remove that data ?

2

There are 2 answers

0
Costique On

I assume you omitted NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; before the quoted code.

You should release image1 before sending [pool drain] because you allocated it. The data object is autoreleased, which means it gets released in [pool drain]. However, releasing the object does not magically set all the pointers to the object to nil, so data points to a deallocated object. Just for kicks, try the following instead of the last line:

NSLog(@"%@", data);

Your app should crash at this line because you can't send messages to deallocated objects.

0
trung nguyen On

Really thanks u, i tested and it's true. This is over view of my problems: I have 132 images in device (~300 kb / 1 image), now my purpose is to merge each 2 images into 1 large image (side by side in horizontal orient). This is what I do :

int index = 1;
for(int i = 1;i <= 132;i++)
{       
    if(i % 2 == 0 && i > 1)
    {                                   
        NSString *file = [NSString stringWithFormat:@"%@img_%d.jpg",path2,index];

        NSLog(@"index %d",index);
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSData *data;
        NSString *filename1 = [NSString stringWithFormat:@"originimg_%d.jpg",i];
        NSString *filename2 = [NSString stringWithFormat:@"originimg_%d.jpg",i + 1];
        NSString *file1 = [[NSBundle mainBundle] pathForResource:filename1 ofType:nil];
        NSString *file2 = [[NSBundle mainBundle] pathForResource:filename2 ofType:nil];

        UIImage *image1 = [[UIImage alloc]initWithContentsOfFile:file1];
        UIImage *image2 = [[UIImage alloc]initWithContentsOfFile:file2];

        UIImage *image = [self combineImages:image1 toImage:image2];                                
        data = UIImageJPEGRepresentation(image, 0.7);               
        [data writeToFile:file atomically:NO];

        [image1 release];
        image1 = nil;
        [image2 release];
        image2 = nil;                               

       [pool drain];    
       pool = nil;          
       [file release];
       file = nil;                              
       index++;
    }   
}           

and function to combine 2 images

-(UIImage *)combineImages:(UIImage *)image1 toImage:(UIImage *)image2 
{   
    CGSize size;    
    size= CGSizeMake(768 * 2, 1024);
    UIGraphicsBeginImageContext(size);

    // Draw image1
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

    // Draw image2
    [image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, image2.size.height)];

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();    
    return resultingImage ;
}
  • That is my way but when I run in instruments (Allocations) It takes 303.4 mb for that :( . Can u suggest me a better way?