UIImage Object surprisingly returning null but not NSData

Posted by riyaz on Stack Overflow See other posts from Stack Overflow or by riyaz
Published on 2012-10-05T15:07:44Z Indexed on 2012/10/05 15:37 UTC
Read the original article Hit count: 186

Filed under:
|
|
|
|

i have created a sqlite db.

and i have insert a few datas in my db..

        UIImage * imagee=[UIImage imageNamed:@"image.png"];
    NSData *mydata=[NSData dataWithData:UIImagePNGRepresentation(imagee)];
    const char *dbpath = [databasePath UTF8String];

NSString *insertSQL=[NSString stringWithFormat:@"insert into CONTACTS values(\"%@\",\"%@\")",@"Mathan",mydata];

 NSLog(@"mydata %@",mydata);

sqlite3_stmt *addStatement;

const char *insert_stmt=[insertSQL UTF8String];
if (sqlite3_open(dbpath,&contactDB)==SQLITE_OK) {
    sqlite3_prepare_v2(contactDB,insert_stmt,-1,&addStatement,NULL);

    if (sqlite3_step(addStatement)==SQLITE_DONE) {

        sqlite3_bind_blob(addStatement,1, [mydata bytes], [mydata length], SQLITE_TRANSIENT);
        NSLog(@"Data saved");

    }
    else{


        NSLog(@"Some Error occured");
    }

    sqlite3_close(contactDB);
}
else{
    NSLog(@"Failure");
}

have written some codes to retrive the data

  sqlite3_stmt *statement;
      if (sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK) {
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM contacts"];


    if (sqlite3_prepare_v2( contactDB, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            char *field1 = (char *) sqlite3_column_text(statement, 0);
            NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
            NSLog(@"UserName %@",field1Str);

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




            UIImage *newImage = [[UIImage alloc]initWithData:data];



            NSLog(@"Image OBJ %@",newImage);
             NSLog(@"Image Data  %@",data);
        }
        sqlite3_close(contactDB);
    }
}

sqlite3_finalize(statement);

the problem is

in log, inserted NSData object and retrieved NSData Objects are different (printing in log gives different stream)

moreover Image OBJ is printed null in log..

Have seen similar questions in stackoverflow. But nothing seems to help.

Please give some suggestions to overcome this issue.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c