【发布时间】:2011-09-28 11:46:51
【问题描述】:
我需要检查项目导航器中是否存在图像。如果没有,则需要从 Internet 下载。目标是能够使用像 [UIImage imageNamed:@"23451.jpg"];
这样的图像所以我需要检查图像是否存在,如果不下载它。我尝试按以下方式执行此操作:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//Set image
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"12345.jpg"];
//Check if the image exists at a given path
BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
//If the image doesn't exists download it.
if (!imageExists)
{
NSData* data = UIImageJPEGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.mywebsite.com/12345.jpg"]]]);
[data writeToFile:imagePath atomically:YES];
}
如果一切顺利,图像将保存在上述路径中。但我仍然无法将此图像与
一起使用[UIImage imageNamed:@"12345.jpg"];
我感觉我将图像保存在不同的目录中,而不是我应该将它们保存到的目录中。 你知道我应该怎么做吗?
【问题讨论】:
标签: iphone objective-c xcode uiimage