如何将图像存储在文档目录中,将图像路径存储到ios中的数据库中?

时间:2016-02-03 07:04:11

标签: ios

我有一个NSMutableArray包含在我的应用程序中从胆汁中选择的图像列表。 数组包含如下图像信息

AlbumImages(

"<UIImage: 0x7fe8a601bf60>, {4288, 2848}",
"<UIImage: 0x7fe8a3f04f10>, {3000, 2002}",
"<UIImage: 0x7fe8a3cb4710>, {1668, 2500}",
"<UIImage: 0x7fe8a3e05c20>, {3000, 2002}",
"<UIImage: 0x7fe8a3cb2e10>, {4288, 2848}",
"<UIImage: 0x7fe8a6125510>, {4288, 2848}"

请建议我如何将此图像存储到数据库中。 我想将图像路径存储到数据库表中并将图像存储到文档目录

4 个答案:

答案 0 :(得分:1)

<强>尝试

for(int i=0;i<array.count;i++){

        NSString *docdirectoryPath = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];

        NSData* data = UIImageJPEGRepresentation(yourimage, 0.8);

        NSString *imgPath = [NSString stringWithFormat:@"%@/imagename%d.png", docdirectoryPath,i];
        [data writeToFile:imgPath atomically:YES];

        //here write your insert query below. and store imgPath in your database

    }

答案 1 :(得分:0)

1 - 为图像创建唯一名称。 2 - 将图像转换为NSData对象,并将writeToFile方法调用为

 java Readfile  Q1.txt

答案 2 :(得分:0)

如果您使用核心数据,请将数据属性设置为外部,并自动将这些内容外部化。无需手动担心名称

答案 3 :(得分:0)

for (int i = 0; i < array.count; i++) {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];

    /***************Save Image in Document Directory*****************/

        UIImage *img = [array objectAtIndex:i];

          NSData *imageData = UIImagePNGRepresentation(img); //convert image into .png format.

          NSFileManager *fileManager = [NSFileManager defaultManager];

          NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",TimeStamp]];


          [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

          NSLog(@"image saved");
        /*****************************************************/
        /***************Save image in  Table*****************/

          NSString *query;

          query=[NSString stringWithFormat:@"insert into table_name values(null,'%@');",fullPath];

          //Execute the query.
          [self.dbManager executeQuery:query];
}