我有基于navigationController的应用程序,它将许多图像存储在文档目录文件夹([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
)中。我还使用sqlite.db在本地存储图像。我的图像存储过程如下:
我的网络服务返回图像数组(网址路径)
我将url图像转换为数据并将转换后的图像数据存储到文档目录文件夹
图片路径也保存在我的sqlite db中。
我的问题是当我将多个图像插入文档文件夹时,我的所有动画效果都会停止或丢失。另一个问题是将图像URL保存到数据(NSData
)是时间过程(我的视图有时冻结)。如何解决这个问题?请通过以下代码
//My viewController class NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSMutableArray * mMsgArray = [[NSMutableArray alloc]init]; for (int i=0; i<[imageArray count]; i++) { Messages *msg = [[Messages alloc]init]; NSSting *imgeUrlPath = [[imageArray objectAtIndex:i]objectForKey:@“imgPath”]]; NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[imageArray objectAtIndex:i]objectForKey:@“imgPath”]]]; msg.msgLocalPath = [self saveImage:data withFileName:[self getCurrentDateTimeAsNSString] ofType:@"png" inDirectory:documentsDirectoryPath]; [mMsgArray addObject:msg]; } BOOL msgsuccess = NO; msgsuccess = [[DataBaseManager getInstance]saveMessage:mMsgArray]; if (msgsuccess == NO) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message insertion failed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; }
//getting different imageName
-(NSString*)getCurrentDateTimeAsNSString
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyyMMddHHmmss"];
NSDate *now = [NSDate date];
NSString *retStr = [format stringFromDate:now];
return retStr;
}
//Saving image to documents path
-(NSString*)saveImage:(NSData *)dataImage withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
NSString *localImgpath = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]];
[dataImage writeToFile:localImgpath atomically:YES];
return localImgpath;
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[dataImage writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
return nil;
}
&GT;
//DataBaseManager Class(Insert message local path to sqlite db)
-(BOOL)saveMessage:(NSMutableArray*)MsgArray;
{
BOOL insertSuccess=NO;
const char *dbpath = [_databasePath UTF8String];//14
sqlite3_stmt *mstatement;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
for (int i=0;i<[MsgArray count];i++)
{
Messages *Msg = [MsgArray objectAtIndex:i];
NSString *insertSQL = [NSString stringWithFormat:@"insert into MESSAGES(msg_local_path) VALUES(\"%@\")",Msg.msgLocalPath];
const char *insert_stmt = [insertSQL UTF8String];
if (sqlite3_prepare_v2(database, insert_stmt,-1, &mstatement, NULL)!= SQLITE_OK){
insertSuccess = NO;
}
else
{
//Add a statement parameter to a query.
if (sqlite3_step(mstatement) == SQLITE_DONE)
{
NSLog(@"SQLITE_DONE");
// sqlite3_reset(mstatement);
insertSuccess = YES;
}
else
{
NSLog(@"SQLITE_FAIL");
NSLog(@"Insert failed: %s", sqlite3_errmsg(database));
sqlite3_reset(mstatement);
insertSuccess = NO;
}
}
}
sqlite3_close(database);
}
else{
insertSuccess = NO;
}
return insertSuccess;
}