【问题标题】:How to retrieve image from image gallery and save the path of it in local database in iphone?如何从图片库中检索图像并将其路径保存在 iphone 的本地数据库中?
【发布时间】:2012-09-19 10:31:53
【问题描述】:

我想知道如何从图库中检索图像并将其路径保存在本地数据库中。在此过程之后,我希望我使用了本地数据库图像路径中的图像。请帮忙!

谢谢。

【问题讨论】:

  • 魔鬼总是在细节中。所以帮助我们。什么样的数据库?
  • 对不起...我正在使用 sqlite 数据库。我希望我将图像的路径存储在其中,以便将来从该路径获取图像。
  • 更具体....问题不止一个。那么你可以从这里stackoverflow.com/questions/11580918/… 开始从 iPhone 画廊获取所有图片 URL ..

标签: iphone objective-c xcode image


【解决方案1】:

关于 sqlite 和图像

在 sqlite 中存储路径(而不是图像)是有意义的。或者你可以考虑使用 Core Data。现在,Core Data 提供了在外部存储中存储图像或其他大数据的能力(基本上消除了查找自己的目录、获取路径、存储路径等的需要),我会喜欢它。

下面的代码假设您可以处理 sqlite 的杂乱细节,或者使用一些库作为原始实现的包装器。

在本地存储图像

您可以决定要将图像存储在应用文件系统中的哪个位置。假设您要使用文档目录:

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

为了说明目的,我们还假设这些是 png 格式:

- (NSString *)pathForImageNamed:(NSString *)imageName {
    return [[self applicationDocumentsDirectory] stringByPathComponent:imageName];
}

- (void)saveImage:(UIImage *)image name:(NSString *)name {
    NSString *path = [self pathForImageNamed:imageName];
    [UIImagePNGRepresentation writeToFile:path];

    // here, save path to sqlite
}

检索图像

- (UIImage *)imageWithName:(NSString *)imageName {
    NSString *imagePath = nil;

    // retrieve path name from sqlite db based on image name

    return [UIImage imageWithContentsOfFile:imagePath];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2021-12-02
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多