2012-08-15 78 views
0

我有一個(可能)非常緩慢和低效的拼貼,我在特定的視圖控制器上生成。它需要永久加載,所以我正在尋找建議,以加快我的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); 
} 

回答

0

爲了在後臺運行此方法(即「後視圖做負載」),我用下面的代碼在viewDidLoad

[self performSelectorInBackground:@selector(obtainImagesForWines) withObject:nil]; 
+0

其實也沒什麼,這是什麼方法做的是,它開始加載在後臺線程中的圖像,使得主界面犯規被封鎖。這就是爲什麼它看起來像在viewDidLoad之後執行它,但實際上它是沿着viewDidLoad執行的。另外我不認爲讓它們成爲blob是一個很好的主意,因爲你不能利用它們作爲資產,像快速縮略圖視圖來創建拼貼。 – Pochi 2012-08-16 00:11:09

+0

你有替代解決方案嗎? – lemontwist 2012-08-16 00:18:59

+0

除非以另一種方式存儲圖像否,這將適用於您的情況。因爲你也不需要太多的控制它的執行。 – Pochi 2012-08-16 02:13:11

1

你可以嘗試瘦身圖像時您要顯示/存儲起來。我不確定,但我認爲你在圖像視圖中顯示了一個非常大的圖像。我相信你正在試圖展示一個16張圖片縮略圖。如果是這樣,請嘗試壓縮圖像。

一個可能更好的選擇,當您存儲圖像時,您可以將實際圖像與縮略圖版本一起存儲,因此當您顯示較小的圖像時,請使用較小的圖像。等等。

UIImageJPEGRepresentation(image,compressionRatio); 

這應該允許您壓縮圖像。或者如果你想像這樣調整它們:

UIImage *yourImage = [UIImage imageWithData:yourdata]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage]; 
UIImageView.frame = CGRectMake(0, 0, yourImage.size.width/2, yourImage.size.height/2); 

希望這個幫助!

更新:

似乎有要對使用SQLite的BLOB存儲圖像的周圍很多計算器論證,並有這裏涉及到一個巨大的性能問題。一個這樣的例子here

我有多個場景,我必須保存圖像。我所做的(我所有的解決方案)都是將圖像保存到文檔文件夾中,然後使用核心數據(或者,如果您願意,可以使用sqlite)存儲對圖像位置和名稱的引用。這樣,從DB讀取的時間就會降低,時間也會縮短。

我用下面的函數作爲一種通用的方法來獲得文件位置:

- (NSString *)getFileLocation { 
    // Get all available file system paths for the user 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    // Get the "Documents" directory path - it's the one and only on iPhone OS 
    NSString *documentsPath = [paths objectAtIndex:0]; 

    // Specify the file name, appending it to documentsPath above 
    NSString *savedFileName = [documentsPath stringByAppendingPathComponent:@"Location.plist"]; 

    // We're done 
    return savedFileName; 
} 

文件位置,然後保存到數據庫的字符串。我會建議使用Core Data來獲得更好的性能。

+0

我不認爲這是必然,這是一個問題,因爲我在當有0張照片保存在那裏的酒廠加載此視圖控制器的情況下得到一個非常長的延遲甚至是圖像的大小=?這時候有0圖片的時候有16.我敢肯定,壓縮圖片是一個好主意,但如果有1000個,如果他們是大或小,在一定程度上也沒關係加載需要一樣長。 – lemontwist 2012-08-15 18:14:30

1

您應該增加,這將需要一段時間的代碼到另一種方法如ViewDidAppear 或者請在後臺運行它,當您嘗試從後臺線程中udpate UI時會導致更多麻煩。所以我會選擇第一個選項並將代碼添加到ViewDidAppear函數中。

如果你想我可以張貼代碼在後臺運行它,但我會誤導你,我會推你的軌道,這將迫使你做更多的修改您的代碼。

相關問題