如何在iphone中的sqlite数据库中插入图像 [英] how to insert image in sqlite database in iphone

查看:69
本文介绍了如何在iphone中的sqlite数据库中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中插入图像,我尝试但不插入。查询中的问题在哪里?这是我的模型类.m文件:此文件中的代码 sqlite3_prepare_v2 我的跟踪没有插入 sqlite3_bind_blob 它是在 sqlite3_prepare_v2 之后直接到外面。这有什么问题,请帮帮我。这里存在保存数据的错误。在模型类中,我创建 UIImage * Photo; 并制作协议和sys

How to insert image in database, I tried to but its not inserting. Where is the problem in query? This is my model class .m file: code in this file after sqlite3_prepare_v2 my trace is not inserting in sqlite3_bind_blob it is going directly outside after sqlite3_prepare_v2. What is the problem here, please help me. Something is wrong here for saving the data. In model class i create UIImage* Photo; and make protocol and sys

+(void)SaveImagesToSql:(NSData *)imgDat :(int)getid
{
    NSString *filePath = [BaseModal getDBPath];
    sqlite3 *database;
    sqlite3_stmt *updStmt;
    NSLog(@"%@",filePath);
    if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) 
    { 
        NSString* sqliteQuery = [NSString stringWithFormat:@"UPDATE INTO Peoples (Photo) VALUES (?) where PeopleId =%i",getid];

        if(sqlite3_prepare_v2(database, [sqliteQuery UTF8String] , -1, &updStmt, NULL) == SQLITE_OK)    {

            sqlite3_bind_blob(updStmt, 1, [imgDat bytes], [imgDat length], NULL);

        }
        sqlite3_finalize(updStmt);
    }
    sqlite3_close(database);
}

这是我的view.m文件我从这个页面选择图像并插入in sqlite file

this is my view.m file i am choosing image from this page and insert in to sqlite file

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

    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //UIImageWriteToSavedPhotosAlbum(imageView.image, self, nil, nil);

    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *docDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@", docDirectory];
    NSLog(@"thiss%@",filePath);
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {    
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   
        [picker dismissModalViewControllerAnimated:YES];
        return;
    } 


    [picker release];
    UIImage *image1 =imageView.image;
    NSData *myData = UIImagePNGRepresentation(image1);

    [PeopleModal SaveImagesToSql:myData :peopleid]; 

    //[self.parentViewController dismissModalViewControllerAnimated:YES];

    [picker dismissModalViewControllerAnimated:YES];



}


推荐答案

你不应该在db中插入blob,这会使你的db非常慢!!

You should not insert blobs in db, this will make your db very very slow!!

你应该将图像保存在app的文档目录中并在数据库中只保存图像的路径。

You should save the image in documents directory of the app and in the db save only the path of the image.

这篇关于如何在iphone中的sqlite数据库中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆