2011-09-29 81 views
0

請檢查通過給數據格式錯誤下面的代碼應用程序崩潰,同時顯示多個圖像

-(void)addScrollView{ 
[self selectData]; 


scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(5, 00, 320, 480)]; 
int counter=5; 
float y=40.0f; 
int fullLength=[photoArray count]; 
int horizontal=320; 
int vertical=(fullLength/4)*80; 
int c1=1; 
for(int c=0;c<[photoArray count];c++){ 
    PhotoData *d=[photoArray objectAtIndex:c]; 

    if(c1==5){ 
     counter=5; 
     y=y+80.0f; 
     c1=1; 

    } 
    UIImage *img1=[[UIImage alloc]initWithContentsOfFile:d.photoPath]; 
    UIButton* button = [[UIButton alloc] init]; 
    button.tag=c; 

    [button setBackgroundImage:img1 forState:UIControlStateNormal]; 
     [button setFrame:CGRectMake(counter, y, 70.0, 70.0)]; 

     [button addTarget:self action:@selector(showDetail:) 
    forControlEvents:UIControlEventTouchUpInside]; 
     [scrollView addSubview:button]; 
    counter=counter+78.0f; 
    c1++; 
    [button release]; 
    [img1 release]; 
    [d release]; 


} 
[scrollView setContentSize:CGSizeMake(horizontal, vertical+200)]; 


[self.view addSubview:scrollView]; 
[scrollView release]; 

     } 





     -(void)selectData{ 
    //This method is defined to retrieve data from Database 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsPath = [paths objectAtIndex:0]; 
     //Obtained the path of Documennt directory which is editable 

    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"memory.sql"]; 
//memory.sql is sqlite file which is used as local database 
photoArray=[[NSMutableArray alloc]init]; 


NSString *dbPath=filePath; 

sqlite3 *database; 

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 

    // Setup the SQL Statement and compile it for faster access 
    const char *sqlStatement = "select * from photo where mid=?"; 
    sqlite3_stmt *compiledStatement; 


    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

     sqlite3_bind_int(compiledStatement, 1,memoryId); 


     while(sqlite3_step(compiledStatement) == SQLITE_ROW) {    
      PhotoData *data=[[PhotoData alloc]init]; 

      int pId=sqlite3_column_int(compiledStatement, 1); 

      NSString *filePath=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 

      [data setPhotoId:pId]; 
      [data setPhotoPath:filePath]; 
      [photoArray addObject:data]; 
      [filePath release]; 


     } // end of the while 


    } 
    sqlite3_finalize(compiledStatement); 
} 
sqlite3_close(database); 

tableArray=[[NSArray alloc]initWithArray:photoArray]; 
paths=nil; 
documentsPath=nil; 
filePath=nil; 
dbPath=nil; 




} 

有些時候,應用程序崩潰

+0

因此,您的錯誤文本也是如此 – Nekto

+0

數據格式化程序暫時不可用,將在'繼續'後重新嘗試。 (未知錯誤加載共享庫「/Developer/usr/lib/libXcodeDebuggerSupport.dylib」) – Ali

回答

1

你不應該釋放被objectAtIndex:如果你還沒有retain編它返回的對象。所以要儘量去除行:

[d release]; 

你應該release它添加到photoArray後該對象。做它行之後:

[photoArray addObject:data]; 
[data release]; 

你應該這樣做,因爲你的data對象不是autoreleasedPhotoData *data=[[PhotoData alloc]init];)並將其添加到photoArray後,它會自動爲retained

+0

試過這個,但又得到了數據格式化錯誤,我用了75張圖片它看起來有些內存問題怎麼樣才能提高它的性能 – Ali

+0

對不起,我從來沒有之前看過這樣的錯誤。 – Nekto

相關問題