Reputation: 187
I am trying to build up my app with the images thumbnails which I loaded from server. The images are in large number. So when I try to run in my iPod it crashes after loading some 50 to 60 images. And the crash is because of the memory leak that I came to know by testing my app with instruments. I have used imageViews and a button for each imageView, and I also released those objects. Here is my code that I have used in my app.
NSInteger result = [self loadedBlipCount] + (_feed.hasMore ? 1 : 0);
if (result == 0 && _emptyFeedCell)
result = 1;
int outer_count = result/3;
scroll_view.backgroundColor = [UIColor whiteColor];
scroll_view.delegate = self;
for (int i=0; i<outer_count; i++)
//if (i<outer_count)
{
xPos = 10;
for (int j=0; j<3; j++) {
_blipyy = [_feed blipAtIndex:count];
image_view = [[URLImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 90, 90)];
image_view.tag = count_tag;
image_view.url = _blipyy.image_tab_images;
UIButton *img_butt = [[UIButton alloc] initWithFrame:CGRectMake(xPos, yPos, 90, 90)];
img_butt.tag = count_tag + 10000;
[img_butt addTarget:self action:@selector(image_tapped:) forControlEvents:UIControlEventTouchUpInside];
xPos = xPos + 95;
count = count + 1;
count_tag = count_tag + 1;
//count_1= count_1 +1;
[scroll_view addSubview:image_view];
[scroll_view addSubview:img_butt];
[image_view release];
[img_butt release];
//[image_view release];
}
// });
yPos = yPos + 95;
}
Please help me with this issue. Thanks in Advance!!
Upvotes: 0
Views: 547
Reputation: 1542
Instead of scroll View take UITableView
and customize UITableViewCell
with your own implementation and re use cells. It will work with out any memory issue.
Upvotes: 1
Reputation: 4500
This problem needs you to change your approach. It's important for you to use either UITableView
(It'll help you handling memory issues). However, if you still want to go ahead and use UIImageView
then I'd suggest you to use this AsyncImageView. To call this API:
ASyncImage *img_EventImag = alloc with frame;
NSURL *url = yourPhotoPath;
[img_EventImage loadImageFromURL:photoPath];
[self.view addSubView:img_EventImage]; // In your case you'll add in your TableViewCell.
It's same as using UIImageView. Easy and it does most of the things for you. AsyncImageView includes both a simple category on UIImageView for loading and displaying images asynchronously on iOS so that they do not lock up the UI, and a UIImageView subclass for more advanced features. AsyncImageView works with URLs so it can be used with either local or remote files.
Loaded/downloaded images are cached in memory and are automatically cleaned up in the event of a memory warning. The AsyncImageView operates independently of the UIImage cache, but by default any images located in the root of the application bundle will be stored in the UIImage cache instead, avoiding any duplication of cached images.
The library can also be used to load and cache images independently of a UIImageView as it provides direct access to the underlying loading and caching classes.
Upvotes: 0
Reputation: 5175
Try SDWebImage - it's works perfect for all my projects and so easy to integrate and use!
https://github.com/rs/SDWebImage
Upvotes: 0