简体   繁体   English

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

[英]How do i retrieve image from SQLite and display in UIImageView

I save all my images in a folder in Xcode and name all the link in SQLite according to the name in the folder. 我将所有图像保存在Xcode的一个文件夹中,并根据文件夹中的名称命名SQLite中的所有链接。 I am using BLOB in SQLite to store the image. 我在SQLite中使用BLOB来存储图像。 How do i get my UIImage to display the image that i want from SQLite? 如何让我的UIImage显示我想要的SQLite图像? It is suppose to be displayed after pressing a character name from the TableView and it will move to the ViewController to display the image. 假设在从TableView中按下字符名称后显示它,它将移动到ViewController以显示图像。

This is the AppDelegate.m file: 这是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

This is the TableViewController.m file: 这是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

And this is the ViewController.m file: 这是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

I have no idea where is the error at and why I am not able to display anything out at all. 我不知道错误在哪里以及为什么我根本无法显示任何内容。

EDIT 编辑

I change my variable for storing the image in SQLite to text and i just input the image name. 我将用于存储SQLite中的图像的变量更改为文本,我只输入图像名称。 In Xcode, I have a image folder with all the images that i need and the name of the images is the same as the one i stored in the database. 在Xcode中,我有一个图像文件夹,其中包含我需要的所有图像,图像名称与我存储在数据库中的图像名称相同。 I used this line of code to retrieve the image name from the database and displayed it out in the UIImage. 我使用这行代码从数据库中检索图像名称并在UIImage中显示出来。

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

Saving the image data in the database is such a bad idea, because there is a big chance to get your database file to become corrupted. 将图像数据保存在数据库中是一个糟糕的主意,因为很有可能使您的数据库文件损坏。 If you want to cache images, better use the approach of downloading and saving those images into your documents folder. 如果要缓存图像,请更好地使用下载方法并将这些图像保存到文档文件夹中。

Just you set like this way I assume you use NSData UIImagePNGRepresentation and then use sqlite3_column_blob instead of sqlite3_column_text . 只是你这样设置我假设你使用NSData UIImagePNGRepresentation然后使用sqlite3_column_blob而不是sqlite3_column_text

Instead of UIImage *aImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 而不是UIImage *aImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

set like this,I hope it will help you. 像这样设置,我希望它会对你有所帮助。

NSData *imageDataFromDb = [[NSData alloc]initWithBytes:sqlite3_column_blob(compiledStatement, 3) length:sqlite3_column_bytes(compiledStatement, 3)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM