views:

123

answers:

3

Hey.

I currently have this code:

UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png", [postsArrayID objectAtIndex:indexPath.row]]]];

It's loading in an image to set in a UITableViewCell. This obviously leaks a lot of memory (I do release it, two lines down after setting the cells image to be that image), and I'm not sure if it caches the image at all.

Is there another way, that doesen't leak so much, I can use to load in images multiple times, like in a tableView, from the Documents-directory of my app? Thanks.

A: 

What is leaking exactly? If you alloc an image and release it after, I don't see the leak your are talking about? Maybe more code or more precision would help.

AngeDeLaMort
That line, that image, is leaking, according to Leaks. I think it was because I forgot to release the image in my custom UITableViewCell's .m file. Something's still leaking, but I think I'll be able to fix it now ;)
Emil
+1  A: 

The leaks tool or Instruments should tell you what exactly is leaking. However, the image and imageView properties retain their images, so you may need to ensure you're properly releasing them in the dealloc method for the UITableViewCell. But like AngeDeLaMort said, we really need more information to give you a precise answer.

jshier
Ah, that's right, I forgot to dealloc them in my UITableViewCell. Thanks.And for record, Leaks only say that "UIImage at this line is leaking."
Emil
Stupid "This comment may no longer be edited"... Ofcourse, I meant to say *release*, not *dealloc*.
Emil
@jshier The images are still leaking, even though I'm releasing both the image created in `cellForRowAtIndexPath:` and the cellImage in my custom cell in the dealloc method. Got any idea why?
Emil
Instrument's Leaks tool should be able to give you a nice stack view of the leak when you run the simulator. Otherwise I can't think of anything else in the code you posted that would cause a leak. Perhaps more context will help.
jshier
A: 

hi i have also the same problem of image showing i want to show multiple iamages in tableview cell or the uiimageview but when i try that the application crashes on device and good on simulator now i dont know what the problem is because i release the image and uiimageview property but still the same crash problem occure so please help me if any one have the solution to this.the code which i have written for getting image are:

NSArray *path1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *filePath1 = [path1 objectAtIndex:0];

pimageName = [filePath1 stringByAppendingPathComponent:[[NSString alloc] initWithFormat: @"%i_%@.png",forward,group1]];

pic = [[UIImage alloc] initWithContentsOfFile:pimageName];

imageView1.image = pic;

[pic release];
Fazalyazdan
You are allocating an NSString on line 3 that should not be allocated. Use this instead. `[NSString stringWithFormat:@"%i_%@.png",forward,group1]`.
Emil
Oh, and the next time you need help, don't post an answer like this to an allready existing question, post a new question instead ;)
Emil