我在特定的视图控制器上生成了一个(可能)非常缓慢且效率低下的拼贴画。加载需要很长时间,所以我正在寻找可以加快我的 SQLite 查询(它正在搜索已保存图像的 BLOB)或导致在视图控制器加载后调用 SQL 查询方法的建议,然后我将放置一个活动加载时的指示器。
这是代码片段。
viewDidLoad
:
- (void)viewDidLoad
{
//generate photo collage
wineryName = theWineOfCurrentWinery.winery;
[self obtainImagesForWines:wineryName];
//lots of other stuff
}
obtainImagesForWines
功能。这是控制显示在 16 个 UIImageViews 之一中的照片,其中有一个 4x4 块:
- (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);
}