我从网上检索图像及其信息(来源和一些其他信息)。我如何将它存储在iPhone上?我看到使用NSFileManager来存储图像,但我怎样才能存储图像和其他一些信息呢?我应该使用NSDictionary和键如 - 图像,起源,信息,然后将该字典存储到NSMutableArray,然后将该数组存储到NSFileManager?或者有其他方法吗?访问这些保存的图像和相应的信息的小提示会很好。在iPhone上保存图像和图像的信息
谢谢。
我从网上检索图像及其信息(来源和一些其他信息)。我如何将它存储在iPhone上?我看到使用NSFileManager来存储图像,但我怎样才能存储图像和其他一些信息呢?我应该使用NSDictionary和键如 - 图像,起源,信息,然后将该字典存储到NSMutableArray,然后将该数组存储到NSFileManager?或者有其他方法吗?访问这些保存的图像和相应的信息的小提示会很好。在iPhone上保存图像和图像的信息
谢谢。
// Download Image from Remote Server
NSURL *url = [NSURL URLWithString:@"Your IMAGE URL HERE"];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"moon" ofType:@"jpg"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath = [paths objectAtIndex:0];
NSString *filePath = [libPath stringByAppendingPathComponent:[url lastPathComponent]];
BOOL status = [data writeToFile:filePath atomically:YES];
// Save Image file to Library Directory, if success then store infor about it in file
if(status)
{
// If File Exist then read it otherwise creat new
NSMutableDictionary *imageInfoDict;
if([[NSFileManager defaultManager] fileExistsAtPath:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]])
{
NSData *fileData = [NSData dataWithContentsOfFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]];
imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]];
// To set As Background Load image from disc
// Read filename from Dictionary, but you must know the key which you want to get
NSString *fileName = imageInfoDict[@"68051_10151509108346729_1731694342_s.png"][@"imagePath"];
// Set your image as background
UIImage *image = [UIImage imageWithContentsOfFile:[libPath stringByAppendingPathComponent:fileName]];
}
else
imageInfoDict = [NSMutableDictionary dictionaryWithCapacity:0];
// Create Dictionary to store Single Image Info
NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:[url lastPathComponent], @"imagePath",
[url lastPathComponent], @"imageName",
[NSDate date], @"date",nil];
// Add Single Image Info to Main Dictionary
[imageInfoDict setValue:imageInfo forKey:[url lastPathComponent]];
// Convert Main info Dictionary to `NSData` to Save on Disc
NSData *fileData = [NSKeyedArchiver archivedDataWithRootObject:imageInfoDict];
// Save file to Disc
[fileData writeToFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"] atomically:YES];
// Read Info From File
NSLog(@"%@",[NSDictionary dictionaryWithContentsOfFile:[libPath stringByAppendingPathComponent:@"imageInfo.plist"]]);
}
非常感谢。这看起来不错,会测试它,并让你知道它是如何工作的。谢谢。 – user1832330
还有一件事,请我真的需要。现在,如果我想从路径访问该图像并将其设置为UIImageView * imgView的背景,我该如何实现这一目标?我怎样才能访问相同的图像“日期”(你设置为例)键?谢谢。 – user1832330
是的日期是示例键,设置为背景我将更新我的代码,但你应该知道你想要设置为背景的图像的关键 –
许多方面都存在不同upong您的要求
1)SQLITE:保存你的图像元数据在表格与文件名一起在一排
2)NSUserDefaults的:如果图像的数量是相当更少
3)文本文件:写入数据由新行分隔的数据,应该也不难,这取决于您的要求。
希望这会有所帮助。
对于userdefault,20张图像太多了? – user1832330
应该没问题。 – Devang
你绝对应该使用CoreData和应用程序文件夹是这样的:
我认为这是最好的和最简单的方法来做到这一点,如果你只是一次下载图像。
如果你想下载不同的图像或更新图像,你可以使用相同的过程,并提到你不应该将图像保存到文档文件夹,你只需要使用一个AsyncImageDownload库(大量)来下载图像,您必须将图像URL存储在数据库中。
尝试此链接http://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-app – iDeveloper
我得到了一些代码,只存储图像,但有关“配对”信息与图像和存储它,要知道哪些信息属于哪个图像,这就是我需要的。不管怎么说,还是要谢谢你。 – user1832330
你是说[存储图像及其exif数据](http://stackoverflow.com/a/5294574/1407017)? – Amar