how to resize image properly without memory warning

229 Views Asked by At

I have stored no of images(For ex : Consider 60+) in document directory. I have to show all those images in table-view. these images are big so i am resizing the images as per this link http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

Here what i make code in cellforrow is :

@autoreleasepool {

            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
            dispatch_async(queue, ^{


                    NSString *folderPath = [self getEduCurrentDiractoryPath];
                    NSString *filePath = [folderPath stringByAppendingPathComponent:[message.internalUrl.absoluteString lastPathComponent]];
                    NSLog(@"currentDirectoryPath %@",filePath);

                    dispatch_sync(dispatch_get_main_queue(), ^{

                        cell.message.thumbnail = [[UIImage imageWithContentsOfFile:filePath] resizedImage:CGSizeMake(210, 170) interpolationQuality:kCGInterpolationLow];

                    });
            });
        }

Now when i check with the INSTRUMENTS to see ALLOCATIONS and tried to find the memory warning cause it shows below result :

So here you can see resizeimage:interpolationQuality method shows 168MB allocation , where My project ARC is on and i have put that code in @autoreleasepool too.. but it is not releasing the allocated memory...

enter image description here

Can any one help me how to rezise the image properly wiout getting memory warning or some can point out where i am making mistake.. Any help will be appreciated..!!! Thanks..

2

There are 2 best solutions below

0
On

Try this. Here imgProf is my image name.

imgProf = [self imageWithImage:imgProf scaledToSize:CGSizeMake(400, 400)];

and this is function created by me to resize image.

- (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
0
On

Do not code it in cellforrow method, U should finish your thumbnail before reloadData.