Ios 如何将sqlite数据库中的图像显示到UIImageView中
我正在使用从这里下载的现有项目 我想在数据库banklist.sqlite3中添加一个名为“image1”的新列作为BLOB,因为我需要在FailedBanksDetailViewController.xib中显示图像 我这样做: 失败的banksdetail.hIos 如何将sqlite数据库中的图像显示到UIImageView中,ios,database,xcode,image,sqlite,Ios,Database,Xcode,Image,Sqlite,我正在使用从这里下载的现有项目 我想在数据库banklist.sqlite3中添加一个名为“image1”的新列作为BLOB,因为我需要在FailedBanksDetailViewController.xib中显示图像 我这样做: 失败的banksdetail.h #import <Foundation/Foundation.h> @interface FailedBankDetails : NSObject { int _uniqueId; NSString *_name;
#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
#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
失败的BankDetailViewController.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
#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
失败的银行数据库.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
// 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"
}
}
// 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
对象(例如,通过UIImagePngPresentation
)然后使用sqlite3\u column\u blob
而不是sqlite3\u column\u text
请不要将二进制数据保存到数据库中。而是将图像写入文档目录,并将该图像的路径存储在数据库中。@bobbypage我个人认为,出于性能原因,我将图像保存在文档目录中,但不知道您是否有其他原因建议这样做……性能真的,在数据库中解决巨大的图像听起来不太合适。这里有更多的讨论:@bobbypage极好的链接。谢谢非常感谢。您能告诉我如何在NSDate*updateDate=[formatter dateFromString:UpdateDateString]下编写代码吗;在FailedBankDatabase.m和[_UpdatedLabelSetText:[formatter stringFromDate:details.UpdateDate]]下?我是新的编码,这将是非常有用的我。