我是 iOS 编程的新手。
通过使用下面的代码,我从数据库中检索图像并将其存储在数组中,然后以缩略图的形式显示这些图像。
通过使用下面的代码,一切正常。但我有两个问题
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
array=[[NSMutableArray alloc]init];
array1=[[NSMutableArray alloc]init];
// Build the path to the database file
databasePath = [docsDir stringByAppendingPathComponent: @"Taukydataaa.db"];
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasePath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent"Taukydataaa.db"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
}
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"select * from tauky"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSString* email_idField = [[NSString alloc] initWithUTF8Stringconst char *) sqlite3_column_text(statement,1)];
NSString* email_idField1 = [[NSString alloc] initWithUTF8Stringconst char *) sqlite3_column_text(statement,0)];
[array addObject:email_idField];
[array1 addObject:email_idField1];
blaukypath =[[NSMutableArray alloc]init];
for (NSString* path in array)
{
[blaukypath addObject:[UIImage imageWithContentsOfFile:path]];
}
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 840.0);
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];
float horizontal = 8.0;
float vertical = 8.0;
for(int i=0; i<[blaukypath count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self actionselector(buttonImagePressed forControlEvents:UIControlEventTouchUpInside];
[buttonImage setImage:[UIImage imageNamed"play.png"] forState:UIControlStateSelected];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
}
[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self actionselector(insertNewObject];
self.navigationItem.rightBarButtonItem = done;
[self.myScrollView addSubview:image];
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
几个观察:
imageWithContentsOfFile
会返回 nil
,以及任何添加 nil
的尝试到一个数组会崩溃。在尝试使用图像之前,请务必进行测试以查看是否成功找到/加载了图像。 UIScrollViewDelegate
方法 scrollViewDidScroll
并且只创建 UIImageView
对象并填充它们各自的 image
滚动到 View 中时的属性(并删除那些已滚动到 View 外的属性)。如果针对 iOS 6,您可以使用 UICollectionView
而不是手动生成的 ScrollView ,您将自动获得其中的一些功能(只要您的数组是图像路径数组而不是图像对象数组)。 didReceiveMemoryWarning
如果不出意外,它会告诉您何时有内存警告,以便您可以识别问题并解决它。并确保在设备上测试这样的内存占用型应用程序,因为有许多与内存相关的问题不会在模拟器上表现出来,但会在设备上抬起头。UICollectionView
,而是您手动构建 ScrollView ,然后您可能希望异步执行此操作。基本思想是,您将有一个后台操作来创建 UIImage
。对象,然后分派(dispatch)将其添加到主队列中的 ScrollView 的 UI 任务(因为您永远不会在后台队列中执行 UI 操作)。但是,这样一来,用户就可以在图像弹出到位时开始使用该应用程序。关于iphone - 在 iphone 中从数据库中检索图像花费的时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878873/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |