Hi,
I have a UITableView where each cell is an image. Only one cell is visible at a time. The images are loaded on demand. The user scrolls to a cell and its image loads.
Thi is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
// this is being compiled for OS 2.0, this is why I am using initWithFrame… it is working fine on 3.0 iPhone.
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 480, 320) reuseIdentifier:MyIdentifier] autorelease];
}
UIImage *imagemX;
UIImageView *myView;
imagemX = [[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat: @"img%d", (indexPath.row+1)] ofType:@"jpg"]] autorelease];
myView = [[[UIImageView alloc]initWithImage:imagemX] autorelease];
[cell addSubview: myView];
return cell;
}
The problem with this code is that the memory use always increases as I scroll thru the table, even if I go back to images previously shown. After scrolling a little bit, the application crashes, probably kicked by springboard by excessive use of memory.
Am I missing something? How can I force an image to release its resources when it is not visible?