Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use an array of UIImages to display large number of images on the screen. Currently I add each image into NSMutableArray and then access them when I need to, but as the number of images go up, the app eventually crashes.

I've tried using Instruments to figure out memory allocation, and it seems the app crashes once the memory reaches around 20MB. Is there a way to get around this? Or any way to conserve memory as much as possible when I'm doing this? Even if you don't have the answer for this, has anyone experienced the similar problem? And if so, how did you compromise/resolve?

By the way, I didn't think of storing to and retrieving from file (or core data) an option, because the app requires quick use of the images, and I had the impression that writing/reading core data or file on iOS was slow compared to when using memory (Please correct me if I am wrong)

share|improve this question
    
while i'm not familiar with xcode, usually the way programs get around running out of memory is to page it to disk ( but that can slow down a program ). Other than that there's either compilation settings or program settings you would have to change. –  Taylor Flores Apr 17 '13 at 2:30
    
Thanks for pointing that out. This app requires quick access to images so I didn't think of storing to file/coredata as an option, just updated the question to reflect this point –  Vlad Apr 17 '13 at 2:35
    
While I can't answer your question as to why its crashing (since you posted no code) I can tell you that I think the design of storing all your images in one array is NOT a good idea. What reason could you have that you need to keep each image space allocated at all times? Store your images, and pick them up as you go, place them back (dealloc) when youre done. The access time of reading the images will most likely not be harmful to the apps speed. –  Max Apr 17 '13 at 3:17
add comment

2 Answers

Yes if you store lot of images in mutable Array then it eat a lot of usable memory. Also you can think about core data. But there is some slow functionality happen on run time you storing and Retrieving data from core data but it safe against memory uses.
I always recommend and use following functionality for such scenario. I cache all images in in document directory and keep its URLs(file location)path in coredata (or any other database). Now on time of presentation i took 3 images from storage current image, previous image and next image. Whenever i show current image then user can scroll and he can see smoothly next or previous image. If user move on next image then i change my those three images. Current image will be now my previous image, next image will be my current image and i load new image from my storage location which will be next image. So got all images in loop and we handle this loop using just 3 images.

share|improve this answer
    
Instead of writing images to Core Data, rather store them in the Cache directory and delete them when not needed any more. That directory is also cleaned up by the system. Writing temporary image data into Core Data or SQLite is just slow, especially for binary/image data that is changing a lot. –  Kerni Apr 17 '13 at 9:34
    
Well, i'm not yet check performance of writing/retrieving blob files inside Core data, SQLite and document directory. I just write my answer in the concern for memory uses while app running. Anyway i mention document directory option for storing blob file to the questioner. @Kerni –  Anonymous Apr 17 '13 at 9:39
    
@iAmbitious Caching images inside of Core Data -- or any other database -- instead of the filesystem is awful for performance and, in some cases, will also significantly increase memory pressure. –  bbum Nov 4 '13 at 16:37
    
@bbum, yes you are correct. Its works good in some cases and some isn't. But apple didn't give us solution (or way to tackle) such kind of problem so that why i'm use my way. –  Anonymous Nov 4 '13 at 16:46
    
@iAmbitious Sure Apple did! Just stick the images in the filesystem next to your database and write the URLs to the files into your database instead of BLOBs. –  bbum Nov 4 '13 at 20:48
show 1 more comment

I had the same problem. Tried different things, eventually hit on storing an array of NSData objects and converting to UIImage on the fly.

for (NSString *imageFile in directoryList) {
    NSString *fileLocation = [imageFolder stringByAppendingPathComponent:imageFile];
    NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
    if (imageData) {
        [images addObject:imageData];
    } else {
        [Util trace:@"cannot get image data for image named: %@", fileLocation];
    }

and then to update your image:

 -(void)updateImageView:(id)sender
{       
    UIImage *anImage = [UIImage imageWithData:[images safeObjectAtIndex:nextImage] ];
    [imageView setImage:anImage];
    nextImage++;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.