iPhone图片库:多次只显示一张图片

时间:2011-11-18 09:59:29

标签: iphone

这是我的代码Roorviewcontrller.h

 @class PhotoDataSource;
 @interface RootViewController : KTThumbsViewController 
 {
     PhotoDataSource *data_;
 }
 @end

Rootviewcontrollr.m文件

@implementation RootViewController
- (void)dealloc 
{
   [data_ release], data_ = nil;
   [super dealloc];
}

 - (PhotoDataSource *)data
 {
   if (data_) {
      return data_;
     }

    data_ = [[PhotoDataSource alloc] init];
   return data_;
 }

- (void)viewDidLoad 
 {
  [super viewDidLoad];

 [self setDataSource:[self data]];
   [self setTitle:[NSString stringWithFormat:@"%i Photos", [data_ numberOfPhotos]]];

     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back button title") style:UIBarButtonItemStylePlain target:nil action:nil];
  [[self navigationItem] setBackBarButtonItem:backButton];
[backButton release];
 }

.h文件

 @interface PhotoDataSource : NSObject <KTPhotoBrowserDataSource>
 { 
 NSMutableArray *data_;
 sqlite3 *UsersDB;
 NSString *databasePath;
 NSString *coun;
 NSMutableArray *sa;
 NSString *fullPathToFile;
 }
@property (nonatomic, assign) NSString *coun;
@end

.m文件

@implementation PhotoDataSource

- (void)dealloc
{
[data_ release], data_ = nil;
[super dealloc];
}

- (id)init
{
self = [super init];
if (self) 
{
  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
  NSString* documentsDirectory = [paths objectAtIndex:0];

 sqlite3_stmt    *statement;
 NSString *docsDir;
 NSArray *dirPaths;
sa = [[NSMutableArray alloc] init];
 dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

   docsDir = [dirPaths objectAtIndex:0];

   databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"Aapke.db"]];
 NSLog(@"%@",databasePath);
 const char *dbpath = [databasePath UTF8String];
   if (sqlite3_open(dbpath, &UsersDB) == SQLITE_OK)
 {
   NSString *querySQL = [NSString stringWithFormat: @"SELECT ImagePath FROM AssetImage",nil];

    const char *query_stmt = [querySQL UTF8String];

   if (sqlite3_prepare_v2(UsersDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
   {

       while (sqlite3_step(statement) == SQLITE_ROW)
       {
           NSLog(@"select");
           coun=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
           NSLog(@"Coun = %@",coun);
           fullPathToFile = [documentsDirectory stringByAppendingPathComponent:coun];
           NSString *imageName1 = [NSString stringWithFormat:@"%@",fullPathToFile];

           NSLog(@"fullpathfile= %@",fullPathToFile);

           NSLog(@"Image Name1=%@", imageName1);
           [sa addObject:fullPathToFile];

           data_ = [[NSMutableArray alloc] init];
           NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageWithContentsOfFile:imageName1], @"thumbnail", [UIImage imageWithContentsOfFile:imageName1], @"fullsize", nil];
           [data_ addObject:dict];
           NSLog(@"dict = %@",dict);
        } 
       sqlite3_finalize(statement);
   }
 }
 NSLog(@"SA=%@",sa);   
}
return self;
}

- (NSInteger)numberOfPhotos
{
NSInteger count = [sa count]; 
[data_ count];
return count;
}

- (UIImage *)imageAtIndex:(NSInteger)index
{
NSDictionary *dict = [data_ objectAtIndex:0];
UIImage *image = [dict objectForKey:@"fullsize"];
return image;
}

- (UIImage *)thumbImageAtIndex:(NSInteger)index
 {
NSDictionary *dict = [data_ objectAtIndex:0];
UIImage *image = [dict objectForKey:@"thumbnail"];
return image;
}
 @end

我执行了这段代码并成功运行。但它显示单个图像。我在iphone sdk路径中存储了多个图像。它没有显示所有图像。我想要显示所有图像。但是此代码仅显示数据库路径中的一个图像与同一图像一样多的图像。如何使用此代码自定义所有图像。有人请事先帮助我。

1 个答案:

答案 0 :(得分:0)

仔细查看您在imageAtIndex:thumbImageAtIndex:中正在做的事情 - 您正在调用objectAtIndex:0,因此您只会看到第一张图片在任何地方使用。而是在两种情况下使用objectAtIndex:index

换句话说,这两种方法应该是:

- (UIImage *)imageAtIndex:(NSInteger)index
{
    NSDictionary *dict = [data_ objectAtIndex:index];  // using index here
    UIImage *image = [dict objectForKey:@"fullsize"];
    return image;
}

- (UIImage *)thumbImageAtIndex:(NSInteger)index
 {
    NSDictionary *dict = [data_ objectAtIndex:index];  // using index here
    UIImage *image = [dict objectForKey:@"thumbnail"];
    return image;
}