AFNetworking和本地图像URL

时间:2014-01-06 07:13:09

标签: ios objective-c afnetworking

我正在尝试使用此代码使用AFNetworking从本地网址获取图片:

    NSString *path = [DOCUMENTS stringByAppendingPathComponent:[NSString stringWithString:@"My Image.png"]];
    [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] placeholderImage:[UIImage imageNamed:@"Placeholder Image.png"] success:nil failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        NSLog(@"%s: setImageWithURLRequest error: %@", __FUNCTION__, error);
    }];

我收到此错误:

NSErrorFailingURLKey=/var/mobile/Applications/6D878789-640E-4299-AA72-45D49211492D/Documents/My Image.png, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x15df8b60 "unsupported URL"}

如何使用AFNetworking执行此操作?

2 个答案:

答案 0 :(得分:0)

尝试不将成功块设置为nil并使用此代码:

[cell.myImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"url"]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
{
            weakimageView.image = image;

          } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
          {
             //Deal with failure
          }];

答案 1 :(得分:0)

试试这个它完美无缺,...

下载UIImageView + webcache.h和.m文件并添加到您的项目中。

链接在这里https://github.com/rs/SDWebImage/blob/master/SDWebImage/UIImageView%2BWebCache.h

NSString *path = [DOCUMENTS stringByAppendingPathComponent:[NSString stringWithString:@"My Image.png"]];

[cell.imageView setImageWithURL:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] placeholderImage:[UIImage imageNamed:@"Placeholder Image.png"]];

或使用Blocks,

NSString *path = [DOCUMENTS stringByAppendingPathComponent:[NSString stringWithString:@"My Image.png"]];

[cell.imageView setImageWithURL:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]  placeholderImage:[UIImage imageNamed:@"Placeholder Image.png"] success:^(UIImage *image, BOOL cached) {
        <#code#>  ---> if success
    } failure:^(NSError *error) {
        <#code#>  ---> if failure
    }];