图像仅显示二进制值

时间:2013-08-22 12:31:40

标签: iphone sqlite uiimageview nsdate

我通过URL将图像存储到数据库表中。我使用后从URL获取该图像。我插入成功。具有URL路径的图像。但是当我提取它时显示二进制值<Mydata: 0x71756b0>。这是什么?

代码:

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        NSData* data = nil;

       // UIImage *image = nil;

       // const char *sql = "SELECT product_image FROM product order by order_by";

         const char *sql = "SELECT product_image FROM product";

        NSLog(@"sql is %s",sql);

        sqlite3_stmt *statement;
      //  int catID = 0;
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.
            while (sqlite3_step(statement) == SQLITE_ROW) {
Mydata *Mylist = [[Mydata alloc]init];

                int length = sqlite3_column_bytes(statement, 2);
                data       = [NSData dataWithBytes:sqlite3_column_blob(statement, 2) length:length];
              //  UIImage *image = [UIImage imageWithData:data];

                 Mylist.photo = [[UIImage alloc] initWithData:data];

                NSLog(@"my list is %@",Mylist);

                [mArray addObject:Mylist];

}
        }
        sqlite3_finalize(statement);
    }

    else {
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }
}

图片显示:

int Width = 0; 

    for (int i = 0; i<[mArray count]; i++ ) {


      NSLog(@"index %@", mArray[i]);



     imgView1=[[UIButton alloc]initWithFrame:CGRectMake(20+(i*74), 0, 72, 72)];

        Width = Width + 20+(i*74);

        [imgView1 setTag:i+1];

        [imgView1 addTarget:self action:@selector(Clicked:) forControlEvents:UIControlEventTouchUpInside];

        [imgView1 setImage:((Mydata *)[mArray objectAtIndex:i]).photo forState:UIControlStateNormal];

        [scrollview addSubview:imgView1];

        //  [myScroll addSubview:imgView1];



    }

1 个答案:

答案 0 :(得分:2)

代替在数据库中存储图像,只在DB中存储URL链接。

使用ASyncImageView从URL获取图像。

因此,只从服务器获取一次图像并在本地存储。