iphone - 如何从SQLite检索图像并在UIImageView中显示

标签 iphone objective-c sqlite xcode4.3

我将所有图像保存在Xcode的文件夹中,并根据该文件夹中的名称在SQLite中命名所有链接。我在SQLite中使用BLOB来存储图像。如何获取我的UIImage以从SQLite中显示所需的图像?
假设它是在按下TableView中的字符名称后显示的,它将移动到ViewController以显示图像。

这是AppDelegate.m文件:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Setup some globals
databaseName = @"Database.sql";

// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];

// Query the database for all records and construct the array
[self readCharactersFromDatabase];
[self readChaptersFromDatabase];
[self readThemesFromDatabase];
//[self readPlotsFromDatabase];

// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}

-(void) readCharactersFromDatabase {
// Setup the database object
sqlite3 *database;

// Init the Array
chars = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select * from Character";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            UIImage *aImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

            // Create a new object with the data from the database
            Character *characters = [[Character alloc] initWithName:aName description:aDescription image:aImage];

            // Add the object to the Array
            [chars addObject:characters];

            //[characters release];
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

}

@end


这是TableViewController.m文件:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [appDelegate.chaps count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Set up the cell

Chapter *chapters = (Chapter *)[appDelegate.chaps objectAtIndex:indexPath.row];

cell.textLabel.text = chapters.name;
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller

Chapter *chapters = [appDelegate.chaps objectAtIndex:indexPath.row];

if(chapView == nil) 
    chapView = [self.storyboard instantiateViewControllerWithIdentifier:@"chapter"];
// self.charView = viewController;


chapView.chapters = chapters;

chapView.chapDes = chapters.description;

[self.navigationController presentModalViewController:chapView animated:YES];

// [charView release];

}

- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];  
self.title = @"Chapter Analysis";
}

-(IBAction)backbutton {
ViewController *mainPage = [self.storyboard instantiateViewControllerWithIdentifier:@"mainPage"];


[self.navigationController presentModalViewController:mainPage animated:YES];
}

@end


这是ViewController.m文件:

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
// self.title = characters.name;
charImage.image =  charI;
charDescription.text = charDes;
[super viewDidLoad];
}

-(IBAction)backbutton {
CharacterTableViewController *characterTable = [self.storyboard instantiateViewControllerWithIdentifier:@"characterTable"];


[self.navigationController presentModalViewController:characterTable animated:YES];
}

@end


我不知道错误在哪里以及为什么我根本无法显示任何内容。

编辑

我将用于在SQLite中存储图像的变量更改为文本,我只输入了图像名称。在Xcode中,我有一个图像文件夹,其中包含我需要的所有图像,图像名称与我存储在数据库中的图像名称相同。
我使用此行代码从数据库中检索图像名称,并将其显示在UIImage中。

charView.image = [UIImage imageNamed:characters.image]; 

最佳答案

将图像数据保存在数据库中是一个坏主意,因为很有可能使数据库文件损坏。如果要缓存图像,最好使用将这些图像下载并保存到文档文件夹中的方法。

关于iphone - 如何从SQLite检索图像并在UIImageView中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11355699/

相关文章:

iphone - 不同设备和模拟器的颜色看起来不同

iOS 7.1 : Border around search bar

java.lang.UnsatisfiedLinkError : org. sqlite.core.nativeDB.open()

java - 如何在我的 Android 应用程序中使用 onItemClick?

iphone - 有没有办法改变模态视图 Controller 外观的动画样式?

iphone - 在外部库协议(protocol)中添加和使用新方法时出现警告

iphone - 如何从 google api 调用获取单个目的地的多条距离路线结果?

ios - 如何在 UITableview 的两行之间添加 UIView

objective-c - NSUndoManager : separate multiple changes in one run loop cycle

sqlite - SQLite 中的 LIKE 和 GLOB 有什么区别?