【问题标题】:How to show image from sqlite database into UIImageView如何将 sqlite 数据库中的图像显示到 UIImageView
【发布时间】:2012-06-05 00:35:01
【问题描述】:

我正在使用从这里下载的现有项目 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 中。

【问题讨论】:

    标签: ios database xcode image sqlite


    【解决方案1】:

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

    【讨论】:

    • 请不要将二进制数据保存到您的数据库中。而是将图像写入您的文档目录并将该图像的路径存储在您的数据库中。
    • @bobbypage 就个人而言,出于性能原因,我将图像保存在我的文档目录中,但想知道您是否有其他原因建议这样做...
    • 性能确实,在数据库中解决巨大的图像听起来不太合适。这里有更多讨论:stackoverflow.com/questions/643682/…
    • @bobbypage 很好的链接。谢谢!
    • 谢谢。你能告诉我代码必须如何在 NSDate *updateDate = [formatter dateFromString:updatedDateString];在 FailedBankDatabase.m 和 [_updatedLabel setText:[formatter stringFromDate:details.updatedDate]] 下; ?我是新编码,这对我来说非常有用。
    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多