2011-03-06 142 views
0

我有一个存储在SQL中的图像(作为数据),我如何在iOS中使用此SQL数据重新创建图像对象?从iOS中获取图像

感谢

+0

这绝对**没有**与Xcode做。 :) – 2011-03-06 00:50:51

+0

发火了?哇,哇。 – bmargulies 2011-03-06 01:10:02

+0

对不起,我的英语。 – Franck 2011-03-06 14:08:34

回答

2

如果您使用SQLitefmdb,会更容易。下面是使用fmdb存储和检索图像的代码。

// let's read in an image from safari's app bundle. 
NSData *safariCompass = [NSData dataWithContentsOfFile:@"/Applications/Safari.app/Contents/Resources/compass.icns"]; 
if (safariCompass) { 
    [db executeUpdate:@"insert into blobTable (a, b) values (?,?)", @"safari's compass", safariCompass]; 

    rs = [db executeQuery:@"select b from blobTable where a = ?", @"safari's compass"]; 
    if ([rs next]) { 
     safariCompass = [rs dataForColumn:@"b"]; 
     [safariCompass writeToFile:@"/tmp/compass.icns" atomically:NO]; 

     // let's look at our fancy image that we just wrote out.. 
     system("/usr/bin/open /tmp/compass.icns"); 

     // ye shall read the header for this function, or suffer the consequences. 
     safariCompass = [rs dataNoCopyForColumn:@"b"]; 
     [safariCompass writeToFile:@"/tmp/compass_data_no_copy.icns" atomically:NO]; 
     system("/usr/bin/open /tmp/compass_data_no_copy.icns"); 
    } 
    else { 
     NSLog(@"Could not select image."); 
    } 

    [rs close]; 

} 
else { 
    NSLog(@"Can't find compass image.."); 
} 

该代码直接从fmdb git回购。

0

从SQL数据库中创建与原始图像数据的NSData对象:

NSData * rawData = [ NSData dataWithBytes: xxx length: xxx ]; 

的从你的数据对象创建的图像:

UIImage * img = [ [ UIImage alloc ] initWithData: rawData ]; 
1

你没有说你如何将图像存储到你的SQL数据库中;我想这是沿着使用UIImageJPEGRepresentation将图像转换为NSData对象,然后以某种方式序列化该NSData对象的行。

要创建来自NSData对象的图像,请使用[UIImage imageWithData:...]。这将以jpeg,png或其他几种格式解析图像数据。