应用程序在显示许多图像时崩溃

时间:2011-09-29 06:27:58

标签: iphone objective-c cocoa-touch

请查看以下代码

   -(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;




}

有时候应用程序因数据格式化程序错误而崩溃

1 个答案:

答案 0 :(得分:1)

如果您没有objectAtIndex:,则不应释放retain返回的对象。因此尝试删除行:

[d release];

将该对象添加到release后,您应该photoArray该对象。在行之后做到:

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

您应该这样做,因为您的data对象不是autoreleasedPhotoData *data=[[PhotoData alloc]init];),将其添加到photoArray后,它会自动retained