【问题标题】:Imageview image save in SQL database using URL in iOS在 iOS 中使用 URL 将 Imageview 图像保存在 SQL 数据库中
【发布时间】:2013-06-30 10:06:04
【问题描述】:

我想将显示的 UIImageview 图像保存在 SQL 数据库中。我正在使用 UIImageview 中显示的相机/相册/库来捕获图像。我想使用 URL 将此图像保存在我的 SQL 数据库中。

我还需要在另一个视图中显示此图像。

如何获取我的 imageview 图像的路径(URL)?如何存储此路径(URL)并存储并显示到另一个视图中?

【问题讨论】:

标签: iphone ios sql


【解决方案1】:

如何抓取 UIImage 并写入内部目录:

UIImageView *imageView = "[your image]";

// define your own path and storage for image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
documentPath = [documentPath stringByAppendingPathComponent:@"test.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 1.0);
[imageData writeToFile:documentPath atomically:YES];

如何获取内部目录中文件路径的 NSURL/NSString:

// specify fileURL as it is an internal file, don't use URLWithString:
NSURL *fileURL = [NSURL fileURLWithPath:documentDirectory];

// store as a string in database
NSString *fileURLString = fileURL.path;

请注意,本地文件的 NSURL 与普通的 NSURL 不同。

// for normal URL
NSURL *webURL = [NSURL URLWithString:"http://www.google.com/"];
[webURL absoluteString]; // -> http://www.google.com/

// for local file path URL
NSURL *localURL = [NSURL fileURLWithPath:"/User/path/samplefile.ext"];
[localURL absoluteString]; // -> file://User/path/samplefile.ext
[localURL path]; // -> /User/path/samplefile.ext

【讨论】:

  • 我已经在 imageview 中显示图像。但我只需要将此显示图像视图路径存储到 sql 并用于另一个视图。我对 sql 部分感到困惑。
  • 我假设您正在使用 SQLite 执行此任务,请查看此处的教程以进行查询、插入或更新:techotopia.com/index.php/…。您只能将字符串类型(用于 URL)存储在数据库中,然后在检索它时尝试再次将其转换回 NSURL。
【解决方案2】:

只需将您的图像名称保存在数据库中并将图像存储在手机内存中......这很容易或者这样试试这条线

UIImage *img = [UIImage imageWithContentsOfFile:(the file path)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    相关资源
    最近更新 更多