【问题标题】:How to show saved image from document directory by getting path through sqlite in iphone如何通过iphone中的sqlite获取路径来显示文档目录中保存的图像
【发布时间】:2012-12-09 04:10:35
【问题描述】:

我已将图像保存在文档目录中并成功将其路径保存在数据库中,现在我不知道如何在我的图像视图中显示该图像?

我应该使用查询来仅选择路径吗?

它会自动从目录中挑选图片吗?

或者我应该在我想要显示图像的视图中使用代码来加载图像。

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *strimagename;
    strimagename=[NSString stringWithFormat:@"Test.jpg"];
    thumbFilePath = [documentsDirectory stringByAppendingPathComponent:strimagename];
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSLog(@"image path: %@ ", [MyCommonFunctions saveImageInDocuments:image]); 
    imageSelect.image = image;
    [self dismissModalViewControllerAnimated:YES];
     NSLog(@"image saved %@",image);
}

以上代码用于保存。


通过使用此代码,我会收到此图像的日志消息:

2012-12-08 20:40:30.450 loginApp[757:c07] CONNECTION SUCCESSFUL WITH DB 
2012-12-08 20:41:05.371 loginApp[757:c07] image path: 08412012084105.png 
2012-12-08 20:41:05.378 loginApp[757:c07] image saved <UIImage: 0x926da00> 

成功查询:

insert into login(Name,Email,Password,DOB,image) values('hamesh', 'hamesh@hamesh.com','hamesh','2012-12-09 04:40:57 +0000','<UIImageView: 0x926d8f0; frame = (139 219; 133 115); opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x926d950>>')

我在这里使用构建 Sql 的代码,在图像中我应该提供属性 NSString 吗?还是 imageView?

- (IBAction)registerbtn:(id)sender {
    DBHandler *db =[[DBHandler alloc]init];

        if([self validateForm])
        {
            if([self validateEmailWithString:txtEmail.text])
            {
                if ([db authenticateRegistration:txtName.text andEmail:txtEmail.text andPassword:txtPassword.text andDob:txtDob.text andImage:imageSelect])
                {
                    UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:@"  Congrats" message:@"SignUp Success" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [loginalert show];
                                }
                else
                { 
 UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:@"  Try Again!" message:@"SignUp Failure" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [loginalert show];
                }
            }
        }

【问题讨论】:

  • 通过使用此代码,我得到了图像 2012-12-08 20:40:30.450 loginApp[757:c07] 与 DB 2012-12-08 20:41:05.371 连接成功的日志消息loginApp[757:c07] 图片路径:08412012084105.png 2012-12-08 20:41:05.378 loginApp[757:c07] 图片已保存 成功查询:插入登录名(姓名、邮箱、密码、DOB、图像)值('hamesh'、'hamesh@hamesh.com'、'hamesh'、'2012-12-09 04:40:57 +0000'、'>')
  • 在与 db 成功连接后查看我的日志消息这不是图像路径吗?
  • 我将您的NSLog 内容放在您的问题中,以便我阅读。请参阅下面的答案。

标签: iphone ios xcode ipad uiimageview


【解决方案1】:

查看包含NSLog 语句的更新问题,您的NSLog[MyCommonFunctions saveImageInDocuments:image] 似乎是正确的。但是您还向我们展示了您正在生成的 SQL,并且显然没有将 [MyCommonFunctions saveImageInDocuments:image] 用作最后一个参数,而是您显然不小心抓住了 UIImageView。查看构建该 SQL 的代码。如果解决方案没有突然出现,请使用构建 SQL 的代码更新您的问题。

【讨论】:

  • 我也用 NSString 代替了 imageView 但出现异常情况
  • @QualityCoder 如果它抛出异常,可能是因为您的NSString 变量设置不正确并且是nil,例如。看起来您正在调用[MyCommonFunctions saveImageInDocuments:image],但我看不到您将返回的文件名保存在哪里。您需要将该文件名放在 NSString 中以供将来参考,这是您在调用 authenticateRegistration 时将使用的 ivar。
【解决方案2】:

你可以使用UIImageimageWithContentsOfFile类方法。

UIImage *image = [UIImage imageWithContentsOfFile:filePathFromDB];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 2011-05-26
    • 2014-09-14
    • 2016-05-19
    • 2020-09-08
    • 2013-10-06
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多