gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:39:05 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com