我有一個(可能)非常緩慢和低效的拼貼,我在特定的視圖控制器上生成。它需要永久加載,所以我正在尋找建議,以加快我的SQLite查詢(它正在搜索已保存圖像的BLOB),或者在查看控制器加載後調用之後調用SQL查詢方法,然後我將加載時的活動指標。加載視圖控制器後如何加載UIImageView
這是代碼片段。
viewDidLoad
:
- (void)viewDidLoad
{
//generate photo collage
wineryName = theWineOfCurrentWinery.winery;
[self obtainImagesForWines:wineryName];
//lots of other stuff
}
obtainImagesForWines
功能。這是控制在16個UIImageViews的一個顯示的照片,還有他們的4×4塊:
- (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);
}
其實也沒什麼,這是什麼方法做的是,它開始加載在後臺線程中的圖像,使得主界面犯規被封鎖。這就是爲什麼它看起來像在viewDidLoad之後執行它,但實際上它是沿着viewDidLoad執行的。另外我不認爲讓它們成爲blob是一個很好的主意,因爲你不能利用它們作爲資產,像快速縮略圖視圖來創建拼貼。 – Pochi 2012-08-16 00:11:09
你有替代解決方案嗎? – lemontwist 2012-08-16 00:18:59
除非以另一種方式存儲圖像否,這將適用於您的情況。因爲你也不需要太多的控制它的執行。 – Pochi 2012-08-16 02:13:11