ios - 如何将 sqlite 数据库中的图像显示到 UIImageView

标签 ios database xcode image sqlite

我使用从这里下载的现有项目 http://www.raywenderlich.com/913/sqlite-101-for-iphone-developers-making-our-app

我想在数据库 banklist.sqlite3 中添加一个名为“image1”的新列作为 BLOB,因为我需要在 FailedBanksDetailViewController.xib 中显示图像。

我这样做了:

FailedBanksDetail.h

#import <Foundation/Foundation.h> 

@interface FailedBankDetails : NSObject {

int _uniqueId;
NSString *_name;
NSString *_city;
NSString *_state;
int _zip;
NSDate *_closeDate;
NSDate *_updatedDate;
NSString *_image1;

}

@property (nonatomic, assign) int uniqueId;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *state;
@property (nonatomic, assign) int zip;
@property (nonatomic, retain) NSDate *closeDate;
@property (nonatomic, retain) NSDate *updatedDate;
@property (nonatomic, copy) NSString *image1;

- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name city:(NSString *)city 
state:(NSString *)state zip:(int)zip closeDate:(NSDate *)closeDate 
updatedDate:(NSDate *)updatedDate image1:(NSString *)image1;

@end

FailedBanksDetail.m

#import "FailedBankDetails.h"

@implementation FailedBankDetails
@synthesize uniqueId = _uniqueId;
@synthesize name = _name;
@synthesize city = _city;
@synthesize state = _state;
@synthesize zip = _zip;
@synthesize closeDate = _closeDate;
@synthesize updatedDate = _updatedDate;
@synthesize image1 = _image1;

- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name 
city:(NSString *)city state:(NSString *)state zip:(int)zip closeDate:(NSDate *)closeDate 
updatedDate:(NSDate *)updatedDate image1:(NSString *)image1 {
if ((self = [super init])) {
    self.uniqueId = uniqueId;
    self.name = name;
    self.city = city;
    self.state = state;
    self.zip = zip;
    self.closeDate = closeDate;
    self.updatedDate = updatedDate;
    self.image1 = image1;
}
return self;
}

- (void) dealloc
{
self.name = nil;
self.city = nil;
self.state = nil;
self.closeDate = nil;
self.updatedDate = nil;
self.image1 = nil;
[super dealloc];
}

@end

FailedBankDetailViewController.h

#import <UIKit/UIKit.h>

@interface FailedBanksDetailViewController : UIViewController {
UILabel *_nameLabel;
UILabel *_cityLabel;
UILabel *_stateLabel;
UILabel *_zipLabel;
UILabel *_closedLabel;
UILabel *_updatedLabel;
int _uniqueId;
UIImageView *_image1View;
}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *cityLabel;
@property (nonatomic, retain) IBOutlet UILabel *stateLabel;
@property (nonatomic, retain) IBOutlet UILabel *zipLabel;
@property (nonatomic, retain) IBOutlet UILabel *closedLabel;
@property (nonatomic, retain) IBOutlet UILabel *updatedLabel;
@property (nonatomic, assign) int uniqueId;
@property (nonatomic, retain) IBOutlet UIImageView *image1View;


@end

FailedBankDetailViewController.m

// In the #import section
#import "FailedBankDatabase.h"
#import "FailedBankDetails.h"

// In the @implementation section
@synthesize nameLabel = _nameLabel;
@synthesize cityLabel = _cityLabel;
@synthesize stateLabel = _stateLabel;
@synthesize zipLabel = _zipLabel;
@synthesize closedLabel = _closedLabel;
@synthesize updatedLabel = _updatedLabel;
@synthesize uniqueId = _uniqueId;
@synthesize image1View = _image1View;

// In the dealloc section AND the viewDidUnload section
self.nameLabel = nil;
self.cityLabel = nil;
self.stateLabel = nil;
self.zipLabel = nil;
self.closedLabel = nil;
self.updatedLabel = nil;
self.image1View = nil;


//In the viewWillAppear method
- (void)viewWillAppear:(BOOL)animated {
FailedBankDetails *details = [[FailedBankDatabase database] 
    failedBankDetails:_uniqueId];
if (details != nil) {
    [_nameLabel setText:details.name];
    [_cityLabel setText:details.city];
    [_stateLabel setText:details.state];
    [_zipLabel setText:[NSString stringWithFormat:@"%d", details.name]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM dd, yyyy"];
    [_closedLabel setText:[formatter stringFromDate:details.closeDate]];
    [_updatedLabel setText:[formatter stringFromDate:details.updatedDate]];
    [_image1View "I DONT KNOW WHAT TO DO HERE"

}
}

FailedBankDatabase.m

// In the #import section
#import "FailedBankDetails.h"

// Anywhere inside the @implementation
- (FailedBankDetails *)failedBankDetails:(int)uniqueId {
FailedBankDetails *retval = nil;
NSString *query = [NSString stringWithFormat:@"SELECT id, name, city, state, 
    zip, close_date, updated_date, image1 FROM failed_banks WHERE id=%d", uniqueId];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) 
    == SQLITE_OK) {
    while (sqlite3_step(statement) == SQLITE_ROW) {
        int uniqueId = sqlite3_column_int(statement, 0);
        char *nameChars = (char *) sqlite3_column_text(statement, 1);
        char *cityChars = (char *) sqlite3_column_text(statement, 2);
        char *stateChars = (char *) sqlite3_column_text(statement, 3);
        int zip = sqlite3_column_int(statement, 4);          
        char *closeDateChars = (char *) sqlite3_column_text(statement, 5);
        char *updatedDateChars = (char *) sqlite3_column_text(statement, 6);
        "I DONT KNOW WHAT TO DO HERE"

        NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
        NSString *city = [[NSString alloc] initWithUTF8String:cityChars];
        NSString *state = [[NSString alloc] initWithUTF8String:stateChars];
        NSString *closeDateString =
            [[NSString alloc] initWithUTF8String:closeDateChars];
        NSString *updatedDateString = 
            [[NSString alloc] initWithUTF8String:updatedDateChars];            
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSDate *closeDate = [formatter dateFromString:closeDateString];
        NSDate *updateDate = [formatter dateFromString:updatedDateString];
        "I DONT KNOW WHAT TO DO HERE"

        retval = [[[FailedBankDetails alloc] initWithUniqueId:uniqueId name:name 
            city:city state:state zip:zip closeDate:closeDate 
            updatedDate:updateDate] autorelease];

        [name release];
        [city release];
        [state release];
        [closeDateString release];
        [updatedDateString release];
        [formatter release];            
        break;            
    }
    sqlite3_finalize(statement);
}
return retval;
}

我对 FailedBanksDetailViewController.xib 没有问题,我从库中放了一个 UIImageView 元素,并将它链接到在 FailedBanksDetailViewController.h 中声明的 image1View。

但我不知道如何设置 FailedBankDatabase.m 和 FailedBankDetailViewController.m。

我需要将图像从 FailedBankDetailViewController 中的数据库显示到 UIImageView 中。

最佳答案

我想你想要一个 NSData 对象作为你的图像(例如通过 UIImagePNGRepresentation),然后使用 sqlite3_column_blob 而不是 sqlite3_column_text.

关于ios - 如何将 sqlite 数据库中的图像显示到 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10821979/

相关文章:

ios - 自定义转换给出 "Unbalanced calls to begin/end appearance transitions"错误

ios - 如何直接从浏览器安装 iOS 应用程序?

sql - 为有异常(exception)的重复事件设计的数据库

ios - 将 pdf 图像转换为 NSData for Parse.com

ios - XCode 不允许我设置应用程序图标/启动图像

ios - 在编辑内容时动态调整 UICollectionViewCell 的大小

iOS:具有一对多关系的核心数据更新对象

ruby-on-rails - PG::InsufficientPrivilege: 错误:必须是数据库的所有者

database - 在 2 个网络服务之间同步 postgres 数据库表

ios - 如何检测 UIStatusBar/iPhone 上的触摸