我是新手,几天来一直在努力解决这个问题。非常感谢您的帮助,以及您的耐心,因为我对此很新。
基本上我们需要做的是:允许用户从他们手机照片库中存储的图像中创建一组图像。我们最终需要访问这些图像并在CollectionView中显示它们。
目前我们的流程如下:我们调用图像选择器方法,获取图像的NSURL,将其保存到数据库中的NSMutable数组。然后我们访问该可变数组并想要填充CollectionView。
从我发现的情况来看,就像这些情况一样:
Saving and retrieving images to and from SQLite in iOS how to insert image in sqlite database in iphone Saving image to Documents directory and retrieving for email attachment
鉴于我们无需保存实际图像或图像数据,它对我们无效。我们只需要某种方式来识别存储在用户手机中的图像,以便我们以后访问它,例如链接,路径或ID。我们尝试了在这些情况下提到的一些代码并得到了相同的错误(对象不能为零)。
我也一直在阅读有关BLOB的信息,以及如果它超过2 kbs可能不是一个好主意,因此我不知道它是不是可行的方法。
而在另一方面,他们提到了一条路径,但它是一个PNGRepresentation,我们试图实现但是得到了同样的错误:
Store images in to sqlite database
这是我们正在使用的代码:
在viewController.m中
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
NSLog(@"Picked image");
UIGraphicsBeginImageContext(CGSizeMake(selectedImage.size.width,selectedImage.size.height));
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
DataBase *dataBase = [[DataBase alloc] init ];
[dataBase addImageToCollection: path];
//Here we were checking all the different kinds of paths
NSLog(@" Image path %@",[path filePathURL]);
NSLog(@" Path %@",[path path]);
NSLog(@" AbsoluteString %@",[path absoluteString]);
NSLog(@" AbsoluteURL %@",[path absoluteURL]);
NSLog(@" Relative path %@",[path relativePath]);
NSLog(@" Relative String %@",[path relativeString]);
//Add image to array for collection
[imagesForCollection addObject:selectedImage];
NSLog(@"Added image to collection");
//Refresh Collectionview
[self.collectionView reloadData];
[selectedImage drawInRect: CGRectMake(0, 0, selectedImage.size.width,
selectedImage.size.height)];
UIGraphicsEndImageContext();
我们目前得到的错误是我们稍后访问的数组是nil。这是日志的输出:
2014-09-13 21:30:04.137 myApp[6603:298943] Got status, it's: 1
2014-09-13 21:30:04.138 myApp[6603:298943] called self.switchState
2014-09-13 21:30:04.138 myApp[6603:298943] urlArray length 2
2014-09-13 21:30:04.154 myApp[6603:298943] In for 1
2014-09-13 21:30:04.172 myApp[6603:298943] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be
nil'
*** First throw call stack:
这是我们在ViewController中调用的Database.m中的代码
-(BOOL) addImageToCollection:(NSURL*)url
{
NSArray *rutas = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [ [rutas objectAtIndex:0] stringByAppendingPathComponent:DATA_BASE_NAME ];
sqlite3 *mySqlite3 = NULL;
if(sqlite3_open([path UTF8String], &mySqlite3) != SQLITE_OK )
{
NSLog(@"Could not access the DB");
}else{
char *consulta3 =" INSERT INTO IMAGESTABLE (ROUTE) VALUES (?) ";
sqlite3_stmt *resultado;
if(sqlite3_prepare_v2(mySqlite3, consulta3, -1, &resultado, nil) == SQLITE_OK)
{
//Saves to DB.
sqlite3_bind_text(resultado, 1, [[url absoluteString] UTF8String], -1, NULL);
}else{
NSLog(@"Error saving absolute String to DB");
}
if(sqlite3_step(resultado) == SQLITE_DONE )
{
NSLog(@"Absolute URL saved");
return true;
}else{
NSLog(@"Error when inserting into DB");
}
sqlite3_close(mySqlite3);
}
return false;
}
感谢。希望有人可以帮助我们解决这个问题!