在我的iPhone应用程序中,我需要捕获图片并将其保存到sqlite数据库中。
NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO ProfileTable (NAME, MOBILE, EMAIL, ProfilePICTURE) VALUES(?, ?, ?, ?)"];
const char *insert = [insertQuery UTF8String];
if(sqlite3_prepare_v2(contactsDB, insert, -1, &insertStatement, NULL) != SQLITE_OK) {
NSLog(@"Error while creating insert Statement");
}
else
{
sqlite3_bind_text(insertStatement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2, [mobile UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2, [email UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(insertStatement, 4, [profilePic bytes], [contactImage length], NULL);
}
}
// Same Query for insert statement also
我以 blob 类型的形式保存了该文件,但这会导致占用大量内存并导致内存警告。
所以我有一个想法。
我想将捕获的图像保存为假设"image1.png"的 png 文件,并将标题"image1.png"插入为 varchar 类型,并在我们想要显示时检索具有该名称的文件。
任何帮助如何做到这一点
获取文档目录(或您可能想要存储图像的其他目录):
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
将图像存储为 png:
NSURL *documentsDirURL = [self applicationDocumentsDirectory];
NSURL *fileURL = [documentsDirURL URLByAppendingPathComponent:@"some-image-name.png"];
NSData* data = UIImagePNGRepresentation(image);
[data writeToURL:fileURL atomically:YES];
NSString *filePath = [fileURL path];
// now save your filePath as string to sqlite
请注意,您必须填写详细信息,例如适当的目录在文件系统中的位置,命名约定,生成文件名,除非它们已经存在等。
编辑:
下面是有关图像串行命名的一些帮助。 有很多不同的解决方案。
在NSUserDefaults
中创建一个包含密钥@"lastImageNumber"
的条目。 当您需要新的映像名称时:
NSInteger imageNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastImageNumber"] + 1;
NSString *imageName = [NSString stringWithFormat:@"image%05d.png",imageNumber];
// save the new imageNumber in the `NSUserDefaults`
[[NSUserDefaults standardUserDefaults] setInteger:imageNum ForKey:@"lastImageNumber"];
在 appDelegate.h 文件中添加 int 计数器
同时添加在应用程序中完成启动与选项方法:
counter = 0;
if(![[NSUserDefaults standardUserDefaults] integerForKey:@"ImageNumber"])
{
[[NSUserDefaults standardUserDefaults] setInteger:counter ForKey:@"ImageNumber"]
}
else
{
counter = [[NSUserDefaults standardUserDefaults] integerForKey:@"ImageNumber"];
}
同时在 appDelegate.h 文件中添加此方法
+ (NSString *) applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
现在,当在数据库中添加捕获的图像时,首先添加int Doc目录,如下所示:
appDelegate = (appDelegate *)[[UIApplication sharedApplication] delegate];
NSString *strDocDirPath = [appDelegate applicationDocumentsDirectory];
NSString *fileName = [NSString stringWithFormat:@"Image%03d.png",appDelegate.counter];
strDocDirPath = [strDocDirPath stringByAppendingPathComponent:fileName];
NSData* imgdata = UIImagePNGRepresentation(yourCapturedImage);
[imgdata writeToFile:strDocDirPath atomically:YES];
appDelegate.counter ++;
[[NSUserDefaults standardUserDefaults] setInteger:appDelegate.counter ForKey:@"ImageNumber"];
//add in database with fileName
像这样检索:
appDelegate = (appDelegate *)[[UIApplication sharedApplication] delegate];
NSString *strDocDirPath = [appDelegate applicationDocumentsDirectory];
strDocDirPath = [strDocDirPath stringByAppendingPathComponent:@"Image001.png"]; //use your database image name here
UIImage *img = [[UIImage alloc]initWithContentsOfFile:strDocDirPath];
每次您都可以创建一个随机字符串并将捕获的图像重命名为随机生成的字符串。
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity:6];
for (int i=0; i<6; i++)
{
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
}
NSString *fileName = [NSString stringWithFormat:@"%@.png",randomString];
希望这会有所帮助。