简体   繁体   English

UIImage对象意外返回null,但未返回NSData

[英]UIImage Object surprisingly returning null but not NSData

i have created a sqlite db. 我创建了一个sqlite数据库。

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) 在日志中,插入的NSData对象和检索到的NSData对象是不同的(日志中的打印给出了不同的流)

moreover Image OBJ is printed null in log.. 此外,图像OBJ在日志中打印为空。

Have seen similar questions in stackoverflow. 在stackoverflow中已经看到了类似的问题。 But nothing seems to help. 但是似乎没有什么帮助。

When you do this: 执行此操作时:

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

You are telling the compiler that the two objects are different, so when you print them, they will always be different. 您告诉编译器这两个对象是不同的,因此当您打印它们时,它们将始终是不同的。 Guaranteed. 保证。 Since they aren't the same object. 由于它们不是同一对象。

I'm not sure why newImage would display null though since you just allocated the object. 我不确定为什么newImage将显示为null,因为您刚刚分配了对象。

You should perform sqlite3_step after sqlite3_bind_blob , not before. 您应该执行sqlite3_step sqlite3_bind_blob不前。 You're inserting before you tell it what to insert! 在插入之前,先告诉您要插入的内容!

Checking results of sqlite_prepare_v2 and sqlite3_bind_blob would be prudent, too. 检查sqlite_prepare_v2sqlite3_bind_blob结果也是谨慎的。

I'd also suggest (unrelated to your question) sqlite3_finalize before you sqlite3_close in both snippets of code. 我也建议(无关你的问题) sqlite3_finalize你之前sqlite3_close的代码都片段。 And if you have an error, sqlite3_errmsg is a life saver, telling you precisely what went wrong, rather than guessing. 如果有错误, sqlite3_errmsg可以sqlite3_errmsg生命,准确地告诉您出了什么问题,而不是猜测。

There is not a major difference but you can try with this and also you can get the image length in console to detect image is properly responding from database or not: 并没有很大的区别,但是您可以尝试一下,也可以在控制台中获取图像长度,以检测图像是否正确地响应了数据库:

const void *ptr = sqlite3_column_blob(stmt, 1);
int size = sqlite3_column_bytes(stmt, 1);
NSLog(@"%d",size);
data = [[NSData alloc] initWithBytes:ptr length:size];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM