【问题标题】:Save image in database将图像保存在数据库中
【发布时间】:2017-07-01 15:17:23
【问题描述】:

在我的应用中,用户可以添加模板。在这个模板上,他添加了任意数量的图像和文本视图。 他可以选择更改文本视图的属性,如字体名称、字体颜色、字体大小等等。 图像操作,例如将图像旋转到任意角度,在运行时增加/减小大小等等。完成此操作后,用户保存此模板并稍后查看他想要的。 所以我想存储用户执行的所有操作并查看最近的列表。 那么如何做到这一点呢?

【问题讨论】:

    标签: iphone ios


    【解决方案1】:
    sqlite3 *database;
        dbName=@"dataTable.sqlite";
        NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentdir=[documentpath objectAtIndex:0];
        dbPath=[documentdir stringByAppendingPathComponent:dbName];
        sqlite3_stmt *compiledStmt;
       if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){
            NSLog(@"Name:%@,Company:%@,URL:%@",model.personName,model.companyName,model.imgurl);
            const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?,?,?)";
        if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){
            sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT);
            NSData *imageData=UIImagePNGRepresentation(imageView.image);
            sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL);
            NSLog(@"Prepare");
            sqlite3_step(compiledStmt);
        }
    sqlite3_finalize(compiledStmt);
    

    【讨论】:

      【解决方案2】:

      我建议将从磁盘检索图像的密钥保存在您的数据库中。
      之后,使用NSArchiverNSUnarchiver 从磁盘获取图像。

      UIImage *img = [NSKeyedUnarchiver unarchiveObjectWithFile:db.ImgKey];
      

      在 db 中保存图像很繁重,令人沮丧和沉重。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-02-14
        • 2020-06-01
        • 2011-02-02
        • 2015-01-08
        • 2013-02-09
        • 2021-01-22
        • 2015-08-13
        • 1970-01-01
        相关资源
        最近更新 更多