2011-03-06 144 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或其他幾種格式解析圖像數據。