I have a (probably) very slow and inefficient collage that I generate on a particular view controller. It takes forever to load, so I am looking for suggestions that would either speed up my SQLite query (it’s searching BLOBs of saved images) or cause the SQL query method to be called after the viewcontroller loads, and then I’ll put an activity indicator while it’s loading.
Here are code snippets.
viewDidLoad:
- (void)viewDidLoad
{
//generate photo collage
wineryName = theWineOfCurrentWinery.winery;
[self obtainImagesForWines:wineryName];
//lots of other stuff
}
obtainImagesForWines function. This is controlling the photos that show up in one of 16 UIImageViews, there is a 4×4 block of them:
- (void) obtainImagesForWines:(NSString *)theWineryName {
sqlite3_stmt *stmt=nil;
sqlite3 *cruddb;
//sql command
const char *sql = "SELECT photo FROM wines WHERE winery=? AND photo NOT NULL ORDER BY RANDOM() LIMIT 16";
//Open db
sqlite3_open([path UTF8String], &cruddb);
int j=0;
if(sqlite3_prepare_v2(cruddb, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [theWineryName UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(stmt) == SQLITE_ROW) {
UIImage *winePhoto;
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
if (data) {
winePhoto = [UIImage imageWithData:data];
}
switch (j) {
case 0:
image1.image = winePhoto;
break;
case 1:
image2.image = winePhoto;
break;
case 2:
image3.image = winePhoto;
break;
case 3:
image4.image = winePhoto;
break;
case 4:
image5.image = winePhoto;
break;
case 5:
image6.image = winePhoto;
break;
case 6:
image7.image = winePhoto;
break;
case 7:
image8.image = winePhoto;
break;
case 8:
image9.image = winePhoto;
break;
case 9:
image10.image = winePhoto;
break;
case 10:
image11.image = winePhoto;
break;
case 11:
image12.image = winePhoto;
break;
case 12:
image13.image = winePhoto;
break;
case 13:
image14.image = winePhoto;
break;
case 14:
image15.image = winePhoto;
break;
case 15:
image16.image = winePhoto;
break;
default:
NSLog(@"wtf?");
}
j++;
}
}
else {
printf("I can't get the wine photo by ID!!: %s\n", sqlite3_errmsg(cruddb));
}
sqlite3_finalize(stmt);
sqlite3_close(cruddb);
}
In order to run this method in the background (in other words “after view did load”), I used the following code in
viewDidLoad: