0

大家好我是Xcode的新手我正在尝试将图像从UITableView插入到ios 5中的数据库(sqlite3)我尝试了下面的代码

-(void)addItemCUR
{

    if(addStmt == nil) {
        const char *sql = "insert into Table(C_Name, C_Image) Values(?,?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
    }

    sqlite3_bind_text(addStmt, 1, [C_Name UTF8String], -1, SQLITE_TRANSIENT);

    NSData *ImageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(self.C_Image)];
    int returnValue = -1;
    if(self.C_Image != nil)
       returnValue =        sqlite3_bind_blob(addStmt, 3, [ImageData bytes], [ImageData length], NULL);
    else
        returnValue =         sqlite3_bind_blob(addStmt, 4, nil, -1, NULL);



    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        rowid = sqlite3_last_insert_rowid(database);

    //Reset the add statement.
    sqlite3_reset(addStmt);
}

我收到类似的错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString CGImage]: unrecognized selector sent to instance 0xb7c0'
*** First throw call stack:
(0x158f052 0x1720d0a 0x1590ced 0x14f5f00 0x14f5ce2 0xf8b6c 0x5c65 0x3182 0x7030 0x16d71d 0x16d952 0x9f586d 0x1563966 0x1563407 0x14c67c0 0x14c5db4 0x14c5ccb 0x1478879 0x147893e 0xdda9b 0x2ac8 0x2a25)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
(gdb

请任何人帮助我提前解决这个问题。

4

3 回答 3

0

将整个图像存储到数据库中是否有任何特定原因?

我想建议您将图像存储在应用程序的 Documents/Caches 文件夹中,并且只有包含图像名称的位置才会作为 varchar 存储到您的数据库中,而不是将整个图像存储到数据库中。因此,当您下次需要使用图像时,您只需遵循为单个图像存储的路径即可。因此,应用程序也不必费心重复重绘图像,这也将提高应用程序的性能。

下面是练习的示例代码..

- (void)saveImage:(UIImage *)image forPerson:(NSString *)fullName  
{
    // 先生成文件名
    NSString *filename = [fullName stringByAppendingString:@".png"]; // 或 .jpg

    // 获取应用文档目录的路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // 附加文件名并获取完整的图像路径
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:filename];

    // 现在将图像转换为 PNG/JPEG 并将其写入图像路径
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:NO];   

    // 这里你将 savedImagePath 保存到你的数据库
    ...
}

要取回图像并显示...

- (UIImage *)loadImage:(NSString *)filePath  
{
    返回 [UIImage imageWithContentsOfFile:filePath];
}
于 2012-10-15T08:35:34.700 回答
0

尝试将图像存储在本地文档目录中。然后将图像路径存储在 sqlite 中。以下代码将对您有所帮助。

NSData *imageData = UIImagePNGRepresentation(photo);//photo - your image
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/photo.png",documentsDir]; 
[imageData writeToFile:pngFilePath atomically:YES];

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/photo.png", docDirectory];

sqlite3 *database;
@try 
{
    NSString *query = [NSString stringWithFormat:@"INSERT INTO Photos(PhotoPath) VALUES('%@')",filePath];
    NSString *databasePath;
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:@"GolfElite.sqlite"];
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)    
    {
        NSLog(@"DB opened...");
        sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL);   
    }

}
@catch (NSException * e) 
{
    NSLog(@"Exception = %@", e);
}
@finally
{
    sqlite3_close(database);
}
NSLog(@"Photos inserted successfully..");   
于 2012-10-15T08:37:28.783 回答
0

这是一个包含图像信息的示例,此代码是一个演示代码...

- (void) saveAllData {

if(isDirty) {

if(updateStmt == nil) {
const char *sql = "update Coffee Set CoffeeName = ?, Price = ?, CoffeeImage = ? Where CoffeeID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(updateStmt, 1, [coffeeName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(updateStmt, 2, [price doubleValue]);

NSData *imgData = UIImagePNGRepresentation(self.coffeeImage);

int returnValue = -1;
if(self.coffeeImage != nil)
returnValue = sqlite3_bind_blob(updateStmt, 3, [imgData bytes], [imgData length], NULL);
else
returnValue = sqlite3_bind_blob(updateStmt, 3, nil, -1, NULL);

sqlite3_bind_int(updateStmt, 4, coffeeID);

if(returnValue != SQLITE_OK)
NSLog(@"Not OK!!!");

if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));

sqlite3_reset(updateStmt);

isDirty = NO;
}

//Reclaim all memory here.
[coffeeName release];
coffeeName = nil;
[price release];
price = nil;

isDetailViewHydrated = NO;
}
于 2012-10-15T08:40:56.197 回答