Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从gallery中选择的应用程序中的iphone商店图像_Iphone_Database_Image_Uiimagepickercontroller - Fatal编程技术网

从gallery中选择的应用程序中的iphone商店图像

从gallery中选择的应用程序中的iphone商店图像,iphone,database,image,uiimagepickercontroller,Iphone,Database,Image,Uiimagepickercontroller,我在谷歌上搜索了很多,但没有找到需要的答案。我正在从图像库中选择照片,并希望将其存储在我的应用程序中。如果我必须将其存储在数据库中(作为BLOB),那么我必须输入文件名(我没有从库中选择该文件名)。我能把它存放在别的地方吗?请帮忙。我被困在这里面很久了。我需要提取图像并在地图视图中显示它 我使用下面的代码从图库中选择照片并插入到数据库中 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickin

我在谷歌上搜索了很多,但没有找到需要的答案。我正在从图像库中选择照片,并希望将其存储在我的应用程序中。如果我必须将其存储在数据库中(作为BLOB),那么我必须输入文件名(我没有从库中选择该文件名)。我能把它存放在别的地方吗?请帮忙。我被困在这里面很久了。我需要提取图像并在地图视图中显示它

我使用下面的代码从图库中选择照片并插入到数据库中

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)dictionary {
        [picker dismissModalViewControllerAnimated:YES];
    tempImg.image = image;
    NSData *imgData = UIImagePNGRepresentation(tempImg.image);
        NSLog(@"the img data %@",imgData);

sql = [NSString stringWithFormat:@"update latlng set image = '%@' where placeName = '%@'",imgData,pName.text];
下面是执行上述sql语句的代码

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement;
        sqlStatement = [sql UTF8String];;

        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
        {
            if (sqlite3_step(compiledStatement))
            {
                NSLog(@"YES");
            }
        }
        else 
        {
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database));
        }
        sqlite3_finalize(compiledStatement);

    }sqlite3_close(database);
下面是要从数据库中提取的代码

NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 5) length:sqlite3_column_bytes(compiledStatement, 5)];
以下代码用于在详图索引中显示图像

UIImageView *mapImg = [[UIImageView alloc] initWithImage [UIImageimageWithData:imgData]];
pinView.leftCalloutAccessoryView = mapImg;

将为照片库获取的图像转换为NSData,然后将其作为Blob保存在数据库中,这样您就可以在需要时轻松地从数据库中检索NSData

NSData *image1Data = UIImagePNGRepresentation(image1.image);
sqlite3_bind_blob(init_statement, 1, [image1Data bytes], [image1Data length], NULL);
image1是ImageView,我在其中显示照片库中的图像


祝您一切顺利。

您可以使用此选项检索应用程序可写目录的路径:

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

有多种方法可以访问该目录,包括NSData的
writeToFile:
方法、NSFileManager类、NSFileHandle类,甚至还有C stdio函数。

在我的例子中,我搜索了很多谷歌,最后得到了下面代码的解决方案。它的工作是为我编写的,希望它能为你工作

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{


NSLog(@"info: %@",info);

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *imgePath = [documentsDirec stringByAppendingPathComponent:@"note.sqlite"];

if(sqlite3_open([imgePath UTF8String], &database) == SQLITE_OK){

    const char *sql = "insert into images (images) values (?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
        UIImage *edtedImae = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *dataForImage = UIImagePNGRepresentation(edtedImae);
        sqlite3_bind_blob(addStmt, 1, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);
    }
    if(sqlite3_step(addStmt) != SQLITE_DONE){

        NSLog(@"error %s",sqlite3_errmsg(database));
    }
    else {
        NSLog(@"Insert row Id = %d",sqlite3_last_insert_rowid(database));
    }
    sqlite3_finalize(addStmt);

    }
   sqlite3_close(database);               
   [picker dismissModalViewControllerAnimated:YES];
}

谢谢你

谢谢你的回答。我现在试了上面的方法。但在转换为nsdata时,应用程序崩溃。我在那里放了一个nslog然后试了试。它显示图像正在加密,但不会停止。然后崩溃。plz帮助。上面的代码适用于少数图像。但对少数人来说,它是有用的。是因为尺寸吗?。我已经用代码编辑了我的问题,我没有任何崩溃日志。它只是没有信息的峭壁。我在日志末尾看到的是27a731a6 83cc8c89 60267940 8之类的数字。在我的情况下,它工作正常,我也检查了您的代码。工作正常。你确定你没有泄漏内存吗。@warrior不,我没有。我查过了。它对我来说也很好,但当我选择从互联网下载的几张图片时崩溃了。这可能是因为图像的大小吗?谢谢。您可以告诉我如何将图像存储在我从图像库获得的目录中。您可以使用
uiimagejpegresentation
将其转换为NSData,然后使用NSData的
writeToFile
将其写入,或者您可以使用。或者可能还有其他方法。