我正在尝试从我的缓存目录中加载照片。
这是我将照片写入缓存文件夹的代码:
+(BOOL)writeAData:(NSData *)imageData atURL:(NSURL *)fileUrl
{
return [imageData writeToURL:fileUrl atomically:YES];
}
此代码工作正常,但是当我尝试从缓存文件夹加载代码时遇到问题:
我在块内使用方法 initWithContentsOfURL:
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageUrl];
这是网址的格式
file://localhost/Users/MarcoMignano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/0E8E54D8-9634-41CF-934B-A7315411F12F/Library/Caches/image_cache/Meyer%20Library.jpg
URL 是正确的,但是当我尝试使用上面的方法时,我得到了这个错误:
..... [__NSCFString isFileURL]: unrecognized selector sent to instance 0x8a0c5b0 .....
这是我获取模型网址时的代码:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.photos = [PhotoAlreadyDisplayed getAllPicture];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UITableViewCell class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if (indexPath) {
if ([segue.identifier isEqualToString:@"Show Image"]) {
if ([segue.destinationViewController respondsToSelector:@selector(setImageUrl:)]) {
NSURL *url = [[self.photos objectAtIndex:indexPath.row] valueForKey:@"url"];
[segue.destinationViewController performSelector:@selector(setImageUrl:) withObject:url];
[segue.destinationViewController setTitle:[self titleForRow:indexPath.row]];
}
}
}
}
}
网址有什么问题?我该如何解决?谢谢你
我找到了解决方案: 当我加载模型的 url 时,我需要使用 fileURLWithPath 方法初始化 url:
NSURL *url = [NSURL fileURLWithPath:[[self.photos objectAtIndex:indexPath.row] valueForKey:@"url"]];
现在一切正常,非常感谢您的帮助