Take the 2-minute tour ×

I am new to iOS programming.

By using below code I am retrieving images from database and storing it in array then displaying those images in thumbnail.

By using below code everything is working fine. But I have 2 problem

  1. retrieving images form database is taking too long.
  2. I store more than 8 images means its not displaying images,it is terminating the application.If I take below 8 images its displaying images in thumbnail.

Can any body tell me, What is mistake in this code?

NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];
array=[[NSMutableArray alloc]init];
array1=[[NSMutableArray alloc]init];

// Build the path to the database file
databasePath =  [docsDir stringByAppendingPathComponent: @"Taukydataaa.db"];

NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasePath];

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Taukydataaa.db"];
    success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];

const char *dbpath = [databasePath UTF8String];

sqlite3_stmt    *statement;

    NSString *querySQL = [NSString stringWithFormat: @"select * from tauky"];

    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        while(sqlite3_step(statement) == SQLITE_ROW)

            NSString* email_idField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,1)];
            NSString* email_idField1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)];
            [array addObject:email_idField];
            [array1 addObject:email_idField1];

            blaukypath =[[NSMutableArray alloc]init];

            for (NSString* path in array)
                [blaukypath addObject:[UIImage imageWithContentsOfFile:path]];

            myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)];
            myScrollView.delegate = self;
            myScrollView.contentSize = CGSizeMake(320.0, 840.0);
            myScrollView.backgroundColor = [UIColor whiteColor];

            [self.view addSubview:myScrollView];

            float horizontal = 8.0;
            float vertical = 8.0;

            for(int i=0; i<[blaukypath count]; i++)
                if((i%4) == 0 && i!=0)
                    horizontal = 8.0;
                    vertical = vertical + 70.0 + 8.0;

                buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];

                [buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
                [buttonImage setTag:i];

                [buttonImage setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal];
                [buttonImage addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
                [buttonImage setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateSelected];

                [myScrollView addSubview:buttonImage];

                horizontal = horizontal + 70.0 + 8.0;

            [myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];

            self.navigationItem.leftBarButtonItem = self.editButtonItem;

            UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(insertNewObject:)];

            self.navigationItem.rightBarButtonItem = done;

            [self.myScrollView addSubview:image];


share|improve this question
Storing imagepath in database. –  Aslam Apr 8 '13 at 12:53
Two possible crashes are likely. Files not found or running out memory. Are you getting an exception code and/or error message? –  Rob Apr 8 '13 at 19:03
add comment

1 Answer

up vote 1 down vote accepted

A couple of observations:

  • You've put your building of the scrollview inside the loop where you're reading data from your table. If you had, for examples, nine rows in your database, you'll have 9 scrollviews, the first with one image, the second with two, the third with three, etc., for a total of 45 images. I really doubt that's what you meant to do.

  • You should have one loop that reads the strings from the database (do not create the image at this point ... just store the image path) and populates your array. You can then have a separate loop (ideally, a completely separate routine) that builds your UI. You really should isolate the the UI from the database interaction. And anything that consists of arrays of image objects will be problematic: Only maintain arrays of image paths.

This problem, of creating lots of images that you don't need, would definitely slow down the app and consume memory. Depending upon the size of the images, you could even be running out of memory and crashing.

At the very minimum, you should address this issue.

There are a couple of additional issues:

  • Another potential source of a crash would be if one of the image paths in your array didn't resolve itself to a path of an image. Thus, the imageWithContentsOfFile would return nil, and any attempt to add nil to an array will crash. Make sure you test to see if you successfully found/loaded the image before you try to use it.

  • You didn't say how big the images are, but if they're bigger than 140x140, you really want to contemplate creating screen-resolution renditions of them. If the images are very big, while it can render them at 70x70, you'll use up the memory for the full image. And when showing this many images at the same time, you'll quickly consume your memory if you're not careful about using screen-resolution images.

  • Depending upon how many images you reference in your array, you might even contemplate a model where you handle the UIScrollViewDelegate method scrollViewDidScroll and only create UIImageView objects and populate their respective image properties as they scroll into view (and remove those that have scrolled out of view). If targeting iOS 6, you can use a UICollectionView instead of your manually generated scrollview, and you'll get some of that functionality automatically (as long as your array is an array of image paths and not an array of image objects).

If you're still crashing, you should share with us the particulars of the crash (if you don't tell us what sort of exception/error you got, we're just guessing). Also, if you haven't, implement a didReceiveMemoryWarning that, if nothing else, tells you when you have a memory warning so you can identify the issue and resolve it. And make sure to test memory hungry apps like this on a device, as there are many memory related problems that won't manifest themselves on the simulator, but will rear their head on a device.

While the above focuses on the crash issue, you also asked a performance question. Especially if you're not using the UICollectionView, but rather you're manually building your scrollview, then you might want to do this asynchronously. The basic idea would be that you would have a background operation that would create the UIImage object, and then dispatch the UI task of adding it to the scroll view in the main queue (as you never do UI stuff in a background queue). That way, though, the user can start using the app while the images are popping into place.

There are a few subtle issues that you might want to think about here (doing db interaction on multiple threads requires some careful implementation; making sure UI updates happen on the main queue; etc.), but if you still have performance issues, this is the typical solution.

Still, I'd focus on fixing the crash first, and deal with the performance issue second.

share|improve this answer
add comment

Your Answer


