Reputation: 1284
in my ios app i am trying to load 1000 images in a scroll view, when i run the code it gets crashed. I found it to be the out of memory error. I tried adding 700 images but there is no crash. But i want to add more than 1000 images(around 4000) and to be viewed. how to manage all the images and get them without any crash.
please help me friends
Upvotes: 0
Views: 763
Reputation: 14427
I don't know where your images are coming from but I wrote a custom method (in a class called WebImageOperations) to load images using blocks and GCD:
Here is the Class: WebImageOperations.h
#import <Foundation/Foundation.h>
@interface WebImageOperations : NSObject {
}
// This takes in a string and imagedata object and returns imagedata processed on a background thread
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage;
@end
WebImageOperations.m
#import "WebImageOperations.h"
#import <QuartzCore/QuartzCore.h>
@implementation WebImageOperations
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage
{
NSURL *url = [NSURL URLWithString:urlString];
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.isupport.processsmagequeue", NULL);
dispatch_async(downloadQueue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(callerQueue, ^{
processImage(imageData);
});
});
dispatch_release(downloadQueue);
}
@end
And in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// Change cell.picImageView to your ImageView
cell.picImageView = [WebImageOperations roundedImageView:cell.picImageView];
// Pass along the URL to the image (or change it if you are loading there locally)
[WebImageOperations processImageDataWithURLString:urlString andBlock:^(NSData *imageData) {
if (self.view.window) {
UIImage *image = [UIImage imageWithData:imageData];
cell.picImageView.image = image;
}
}];
That will load the images as needed and the TableView will take care of de-queuing them as needed. Thanks
Upvotes: 2
Reputation: 12493
You have to load the images only when they are in visible frame. If user scrolls away from a loaded image, then you should unload it and release the associated memory. Thats how you will be able to show 1000s of images without getting a crash.
Upvotes: 1