Memory is increasing in bytes when the image is loaded from UIImagePickerController
Actually I am using the stackoverf开发者_StackOverflow社区low for the first time to search solution to my problem.
The problem is that I am using a table view in which in each cell there is an ImageView to show a selected image and a button which opens the image picker controller and allows the user to pick the image from gallery or camera. After selecting the image from the gallery I used the function
NSData *imageData=UIImageJPEGRepresentation(image2, .4);
to convert it into data to reduce the memory size of image and then save it in the documents directory and finally reload the table.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",currentCameraRow]];
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath])
{
NSLog(@"file is exiting and starting");
if ([[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil]) {
NSLog(@"file is exiting and deleting");
}
}
Else I am saving the image in the documents directory and reload the UITable.
After reloading the table, the image should appear in the ImageView of the cell.
Now the problem is that if I again select the image for the same cell and load it after clicking on the camera button, the image will be appear, but the memory will also increase by .15MB which I checked on the Instruments and this makes my app crash on iPhone 3.
What I am actually thinking is that, if I select the image for the same cell irrespective of the number of times I do it, the image should be overwritten in the documents directory and the memory size should not increase when I check in the instrument but this is not happening.
I am really frustrated with this problem, so please anyone help. Your help will be highly appreciated.
If your memory footprint grows, you are most likely leaking some objects, i.e. not releasing them properly. You may check with instruments and the static analyzer.
Please note that compressing the image will not reduce the memory it will consume when loaded. This is just pixels_width * pixels_height * 4 bytes.
Even if you compress the image down so it doesn't use much storage space on disk in a file, when you load it in your app it becomes uncompressed and will take up a considerable amount of memory in the application depending on the resolution.
Try checking for zombies using instruments. There is possibly a leak resulting in memory increase. Also using cache might save you some space when choosing image again and again. Release the objects as soon as they are not needed to free up the memory used.
精彩评论