如何将UIImage插入数据库并从数据库中获取UIImage

时间:2014-02-04 10:26:07

标签: ios sqlite

在数据库中创建表

  const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT, PROFILEPHOTO BLOB)";

我试图将profilephoto数据插入数据库表。

    UIImage *image = [UIImage imageNamed:@"sampleimg.png"];
    NSData *imageData=UIImagePNGRepresentation(image);

    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS(name, address, phone, profilephoto) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")",name, address, phone,imgData];
        const char *insert_stmt = [insertSQL UTF8String];
       if( sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL))
       {

           sqlite3_bind_blob(statement, 4, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
           if (sqlite3_step(statement) == SQLITE_DONE)
           {
               NSLog(@"Contact added");
           } else
           {
               status.text = @"Failed to add contact";

           }
           sqlite3_finalize(statement);
       }

      sqlite3_close(contactDB);
   }

有时插入查询没有执行,但为imagedata配置文件照片获取了一些值  现在尝试访问profilephoto

 if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM CONTACTS"];
        const char *query_stmt = [querySQL UTF8String];
        if (sqlite3_prepare_v2(contactDB,query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                int length = sqlite3_column_bytes(statement, 4);
                NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 4) length:length];

                NSLog(@"Length : %d", [imageData length]);
                UIImage *aImage = [UIImage imageWithData:imageData];
                if(aImage == nil)
                    NSLog(@"No image found.");
                else
                    aImage = [UIImage imageWithData:imageData];

                imgView.image = aImage;

                NSLog(@"Match found");

                NSLog(@"user: %@ addressContact: %@ phoneNum:%@",user1,addressField,phoneField);


            } else
            {
               NSLog(@"Match not found");
                address = @"";
                phone = @"";
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
    }

如何将图像插入数据库以及如何在iOS中正确访问它

2 个答案:

答案 0 :(得分:0)

您的数据插入需要使用NSString:

NSData *imageData=UIImagePNGRepresentation(image);
NSString *imgDataStr = [NSString stringWithUTF8String:[imageData bytes]];

答案 1 :(得分:0)

您可以按照以下方式执行此操作:

我假设你知道使用SQLite进行数据插入和数据获取。

将图片插入数据库:

NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(yourImage, 0.5)];

现在您只需将NSData插入数据库即可。

从数据库中获取图片

使用以下代码从NSData获取图像:

[UIImage imageWithData:imageData]