Ios 存储来自手机的图像集';,从数据库访问它们并在CollectionView中显示它们

Ios 存储来自手机的图像集';,从数据库访问它们并在CollectionView中显示它们,ios,objective-c,image,sqlite,Ios,Objective C,Image,Sqlite,我是个新手,已经为这个问题挣扎了几天。非常感谢您的帮助,也非常感谢您的耐心,因为我对这方面还不太熟悉 基本上,我们需要做的是:允许用户从他们存储在手机照片库中的图像创建一个图像集合。我们最终需要访问这些图像并在CollectionView中显示它们 目前我们的过程如下:调用一个图像选择器方法,获取图像的NSURL,将其保存到数据库中的NSMutable数组中。然后我们访问该可变数组,并希望填充CollectionView 根据我的发现,比如这些案例: 它不适用于我们,因为我们不需要保存实际图

我是个新手,已经为这个问题挣扎了几天。非常感谢您的帮助,也非常感谢您的耐心,因为我对这方面还不太熟悉

基本上,我们需要做的是:允许用户从他们存储在手机照片库中的图像创建一个图像集合。我们最终需要访问这些图像并在CollectionView中显示它们

目前我们的过程如下:调用一个图像选择器方法,获取图像的NSURL,将其保存到数据库中的NSMutable数组中。然后我们访问该可变数组,并希望填充CollectionView

根据我的发现,比如这些案例:

它不适用于我们,因为我们不需要保存实际图像或图像数据。我们只需要一些方法来识别存储在用户手机中的图像,以便我们以后访问它,比如链接、路径或id。我们尝试了在这些情况下提到的一些代码,但得到了相同的错误(对象不能为零)

我也读过关于BLOB的文章,如果它超过2kb,这可能不是一个好主意,因此我不知道这是否是一个好办法

在另一个例子中,他们提到了一条路径,但这是一个PNGRepresentation,我们试图实现它,但得到了相同的错误:

这是我们正在使用的代码:

在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();
我们目前遇到的错误是,我们稍后访问的数组为零。以下是日志的输出:

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;

}
谢谢。希望有人能帮我们解决这个问题