I'm a newbie and have been struggling with this issue for a couple of days. Your help is greatly appreciated, as well as your patience given that I'm fairly new to this.
Basically what we need to do is: allow users to create a collection of images from the ones they have stored in their phone's photo library. We eventually need to access those images and show them in a CollectionView.
Currently our process is as follows: we call an image picker method, get the image's NSURL, save it to an NSMutable array in the database. Then we access that mutable array and want to populate a CollectionView.
From what I've found, like these cases:
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
it does not work for us given that there is no need for us to save the actual images or the image data. We need simply some way to identify the images that are stored in the user's phones in order for us to access it later, like a link, a path or id. We tried some of the code that was mentioned in those cases and got the same error (object cannot be nil).
I've also been reading about BLOB's and how it's probably not a good idea if it's over 2 kbs, hence I don't know if it would be the way to go.
and in this other, they mention a path, but it's a PNGRepresentation, which we tried to implement but got the same error:
Store images in to sqlite database
This is the code we are using:
In the 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();
The error we are getting at the moment is that the array that we access later is nil. Here's the log's output:
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:
Here's the code in Database.m that we are calling in the ViewController
-(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;
}
Thanks. Hopefully someone can help us out with this!