1

我将所有图像保存在 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]; 
4

2 回答 2

3

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

于 2012-07-06T04:29:26.993 回答
0

只是你这样设置,我假设你使用NSData UIImagePNGRepresentation然后使用sqlite3_column_blob而不是sqlite3_column_text.

代替 UIImage *aImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

设置成这样,希望对你有帮助。

NSData *imageDataFromDb = [[NSData alloc]initWithBytes:sqlite3_column_blob(compiledStatement, 3) length:sqlite3_column_bytes(compiledStatement, 3)];
于 2012-07-06T04:45:05.570 回答