Iphone 如何从文档文件夹中获取保存的图像,并在下次访问url时显示图像,而不是从解析的CD图像
我有一个应用程序,我想在本地存储来自在线图像路径(url)的图像图标 当用户访问另一个url时,我将此图像存储在本地,该图标将替换为以前的url,以及如何替换此图标以重复使用plz帮助我解决问题 谢谢大家!Iphone 如何从文档文件夹中获取保存的图像,并在下次访问url时显示图像,而不是从解析的CD图像,iphone,ios,Iphone,Ios,我有一个应用程序,我想在本地存储来自在线图像路径(url)的图像图标 当用户访问另一个url时,我将此图像存储在本地,该图标将替换为以前的url,以及如何替换此图标以重复使用plz帮助我解决问题 谢谢大家! NSString *strImage = [NSString stringWithFormat:@"%@",aBook.image]; NSURL *url4Image = [NSURL URLWithString:strImage]; NSD
NSString *strImage = [NSString stringWithFormat:@"%@",aBook.image];
NSURL *url4Image = [NSURL URLWithString:strImage];
NSData *data = [NSData dataWithContentsOfURL:url4Image];
if(data !=NULL)
{
imageView =[[UIImage alloc] initWithData:data];
if(imageView == NULL)
{
imageView =[UIImage imageNamed:@"dealsbell_lo.png"]; // NoImage.png
}
}
NSData *imageData = UIImagePNGRepresentation(imageView);
NSString *imageName = [NSString stringWithFormat:@"image.png"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:YES];
cell.imageView.image = imageView;
对于这种情况,您应该缓存下载的图像,并为下一个请求恢复相同的图像 我认为您应该使用相同的框架。执行以下操作:
NSString *imageName = [NSString stringWithFormat:[NSString stringWithFormat:@"image_%d.png",indexPath.row]];//change name of image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
if([[NSFileManager defaultManager] fileExistsAtPath:fullPathToFile])
{
NSData *imgData = [NSData dataWithContentsOfFile:fullPathToFile];
if(imgData){
UIImage *img = [UIImage imageWithData:imgData];
cellimageView.image = img;
}
}
else
{
NSString *strImage = [NSString stringWithFormat:@"%@",aBook.image];
NSURL *url4Image = [NSURL URLWithString:strImage];
NSData *data = [NSData dataWithContentsOfURL:url4Image];
if(imgData){
UIImage *img = [UIImage imageWithData:imgData];
cellimageView.image = img;
[data writeToFile:fullPathToFile atomically:YES];
}
}