无法在ios应用程序上显示sqlite db中的图像

时间:2014-02-22 19:57:15

标签: ios sqlite uiimage nsdata

我在iOS应用程序上工作,所以我需要保存用户将从他的相册中选择的图像,或者从相机中取出它。我正在用挑选者做这件事

然后我尝试使用以下代码将图像保存到表中:

- (IBAction)saveUOme:(id)sender
{
    NSString * _name = name.text;
    NSString * _lastname = lastname.text;
    NSString * _email = email.text;
    NSString * _workemail = workEmail.text;
    NSString * _cellphone = cell.text;
    NSString * _phone = homePhone.text;
    NSString * _money = amount.text;
    NSString * _stuff = stuff.text;
    NSString * _cause = cause.text;
    //NSData *_image = UIImagePNGRepresentation(image);
    NSData *_image = [NSData dataWithData:UIImagePNGRepresentation(image)];

    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSString * _date = [dateformatter stringFromDate:[datePicker date]];

    if (!switchR.on)
    {
        _date=@"";
    }
    //NSString *_error;


    NSLog(@" %@ ,%@ ,%@ ,%@ ,%@ ,%@,%@,%@,%@,%@ ", _name,_lastname,_email,_workemail,_cellphone,_phone,_money,_stuff,_cause,_date);


    if(([_name length]!=0 && [_lastname length] !=0 && [_money length] !=0) || ([_name length]!=0 && [_lastname length] !=0 && [_stuff length] !=0 ) )
    {

        NSString *query = [NSString stringWithFormat:@"INSERT INTO addUOmeTable ('name','lastname','email','workemail','cellphone','phone','money','stuff','cause','date','image') VALUES ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@');", _name, _lastname, _email, _workemail, _cellphone, _phone,_money,_stuff,_cause,_date,_image];

        char *err;

        if (sqlite3_exec(_db, [query UTF8String], NULL, NULL, &err) != SQLITE_OK)
        {
            sqlite3_close(_db);
            NSAssert(0,@"No se pudo agregar");
        }else
        {

            NSLog(@"Se agregaron los datos");

        }


    }else
    {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Some Data is Missing"
                                                        message:@"Name, Lastname or Amount/Stuff"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

        /*
        FirstViewController *obj =[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
        [self.view addSubview:obj.view];*/
        //[self.navigationController pushViewController:obj animated:YES];



    }



}

到目前为止,我认为这是有效的,因为在数据库中我将​​其作为保存对象

<89504e47 0d0a1a0a 0000000d 49484452 00000400 00000300 08020000 0035d882 5a000000 1c69444f 54000000 02000000 00000001 80000000 28000001 80000001 80000c6c fd9ea091 34000040 00494441 5478019c 7df7775c 45b6357f ce7b3360 a5ce4959 b2255956 ce9d734e ea56ce96 73c0186c c0806d8c 73ced9c6 e4641be7 086f6698 21e300cc fbf9dbb7 4feb50be 2d9b79df 5a67d5da 75ead4a9 bab7c33d fb567a2e 38520e09 0f574222 4395e1c1 8ae84065 46aac37d 95c1de4a 7fa2dc93 2873c74b 9db1325b acbc275c da1d2eef 8c967644 4ada22a6 d6b0be29 ac6b8ea8 5b23eaf6 88aa3ba6 b624d4ce 84d693d2 85fb0df1 2153efa0 2135a84b 0de953a.....>

然后我尝试使用此代码从数据库中选择所有

-(void) selectData
{
    NSString *query = [NSString stringWithFormat:@"SELECT * FROM addUOmeTable ORDER BY name ASC"];
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(_db, [query UTF8String], -1, &statement, Nil)== SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW)
        {

            char *id1 = (char *) sqlite3_column_text(statement, 0);
            NSString *id_uomeDoubt = [[NSString alloc] initWithUTF8String:id1];

            char *name1 = (char *) sqlite3_column_text(statement, 1);
            NSString *name = [[NSString alloc] initWithUTF8String:name1];


            char *lastname1 = (char *) sqlite3_column_text(statement, 2);
            NSString *lastname = [[NSString alloc] initWithUTF8String:lastname1];

            char *email1 = (char *) sqlite3_column_text(statement, 3);
            NSString *email = [[NSString alloc] initWithUTF8String:email1];

            char *workemail1 = (char *) sqlite3_column_text(statement, 4);
            NSString *workemail = [[NSString alloc] initWithUTF8String:workemail1];

            char *cellphone1 = (char *) sqlite3_column_text(statement, 5);
            NSString *cellphone = [[NSString alloc] initWithUTF8String:cellphone1];

            char *phone1 = (char *) sqlite3_column_text(statement, 6);
            NSString *phone = [[NSString alloc] initWithUTF8String:phone1];

            char *money1 = (char *) sqlite3_column_text(statement, 7);
            NSString *money = [[NSString alloc] initWithUTF8String:money1];

            char *staff1 = (char *) sqlite3_column_text(statement, 8);
            NSString *staff = [[NSString alloc] initWithUTF8String:staff1];

            char *couse1 = (char *) sqlite3_column_text(statement, 9);
            NSString *couse = [[NSString alloc] initWithUTF8String:couse1];

            char *date1 = (char *) sqlite3_column_text(statement, 10);
            NSString *date = [[NSString alloc] initWithUTF8String:date1];

            NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 11) length:sqlite3_column_bytes(statement, 11)];


            doubtArr = [NSArray arrayWithObjects:id_uomeDoubt,name,lastname,email,workemail,cellphone,phone,money,staff,couse,date,data, nil];


            [uomeDoubts addObject:doubtArr];


        }
    }

    [self.myTable reloadData];

}

最后我尝试使用此代码将图像显示到单元格中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if(!cell)
    {
        cell= [[CustomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }


    NSArray *debtor = [uomeDoubts objectAtIndex:indexPath.row];


    NSString *nombre = [debtor objectAtIndex:1];
    NSString *nombre2 = [debtor objectAtIndex:1];
    NSString *apellido = [debtor objectAtIndex:2];
    nombre = [nombre stringByAppendingString:@" "];
    nombre = [nombre stringByAppendingString:[debtor objectAtIndex:2]];

    cell.TitleLable.text = nombre;

    NSString *tempo = [debtor objectAtIndex:7] ;


    if ([tempo length] ==0)
    {
        NSString *st = @"Stuff - ";
        st = [st stringByAppendingString:[debtor objectAtIndex:8]];

        cell.SubtitleLable.text = st;

    }
    else
    {
        NSString *mny = @"Money - $";
        mny = [mny stringByAppendingString:[debtor objectAtIndex:7]];

        cell.SubtitleLable.text = mny;
    }

    NSLog(@"%@ %@",nombre2,apellido);


    NSData* data = [debtor objectAtIndex:11];
    cell.imageView.image = [UIImage imageWithData:data];




    return cell;

}

我没有得到任何错误,唯一的问题是图像根本不显示

请帮助!

0 个答案:

没有答案