2011-09-29 83 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

相关问题