OStack程序员社区-中国程序员成长平台

标题: 来自 sqlite 数据库的 ios 图像显示 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:26
标题: 来自 sqlite 数据库的 ios 图像显示

我已通过单击 saveInfo 按钮将我的图像和文本保存到 sqlite3 数据库中。

- (IBAction)saveInfoid)sender {

    // Prepare the query string.
    NSString *query;

    if (self.recordIDToEdit == -1) {

        query = [NSString stringWithFormat"insert into empInfo values('%@', '%@','%@','%@','%@','%@' )", self.txtEmpCode.text, self.txtName.text, self.txtDesignation.text,self.txtDepartment.text,self.txtTagline.text,self.saveImage.image];
    }

    else{

        query = [NSString stringWithFormat"update empInfo set name='%@',empInfoCode='%@',designation='%@',department='%@',tagLine='%@',photo='%@' where empInfoCode=%d", self.txtEmpCode.text,self.txtName.text, self.txtDesignation.text,self.txtTagline.text,self.txtDepartment.text,self.saveImage.image, self.recordIDToEdit];

    }


    // Execute the query.--------------

        [self.dbManager executeQuery:query];

    //   If the query is sucessfull the show this in the dubugger.
    if (self.dbManager.affectedRows != 0) {

        NSLog(@"Query was executed successfully. Affected rows = %d", self.dbManager.affectedRows);

        // Here we inform the delegate that editing in app is finished.

        [self.delegate editingInfoWasFinished];

        // Pop the view controller.
        [self.navigationController popViewControllerAnimated:YES];
    }
    else{
        NSLog(@"%d",self.dbManager.affectedRows);
        NSLog(@"---Could not execute the query.----");
    }

}

然后我用这个方法加载数据,我可以访问文本数据但是不能用这个方法加载图像。

-(void)loadInfoToEdit{

    // Create the query.

    NSString *query = [NSString stringWithFormat"select * from empInfo where empInfoCode=%d", self.recordIDToEdit];

    // Load the relevant data.

    NSArray *results = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];

    // Setting the loaded data to the textfields and imageView.
.
    self.txtEmpCode.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"empInfoCode"]];

    self.txtName.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"name"]];

    self.txtDesignation.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"designation"]];

     self.txtTagline.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"tagLine"]];

    self.txtDepartment.text = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"department"]];

self.saveImage.image = [[results objectAtIndex:0] objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"photo"]];

  //  NSLog(@"This photo has:%@",[[results objectAtIndex:0]objectAtIndex:[self.dbManager.arrColumnNames indexOfObject"photo"]]);

  //  self.saveImage.image= [[results objectAtIndex:0]objectAtIndex:[self.dbManager.arrColumnNames indexOfObject:@"photo"]];

//    NSLog(@"This Nsdata object has %@",img);
//    self.saveImage.image = [UIImage imageWithData:img];

}

如何将我的图像加载到 saveImage(UIImageView 属性)



Best Answer-推荐答案


您应该将图像保存在文档目录中。为此,您应该首先将图像转换为数据,然后将该数据保存(或写入)到 documentdirectory。并且您应该将图像名称保存到您的 sqlite 数据库

现在,当您从数据库中获取数据时,您将获得 imagename,通过该 imagename 您可以从 document directory 获取您保存的图像数据,然后您可以将其转换数据图像,可以根据需要使用。

例子:

保存图片,

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

 NSString *profilePicturePath = [docsDir stringByAppendingPathComponent:@"profilePicture"];

 NSData *data = UIImageJPEGRepresentation(yourImage, 1.0);

 [data writeToFile:profilePicturePath atomically:NO];

您可以检索图像,

 UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:profilePicturePath]];

关于来自 sqlite 数据库的 ios 图像显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481828/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4