2014-01-06 62 views
1

我試圖使用AFNetworking使用此代碼本地URL獲得的圖像:AFNetworking和本地圖像URL

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做到這一點?

+0

只是一個想法。你試過* [NSURL fileURLWithPath:path] *方法嗎? – EmptyStack

+0

NSURL * url = [NSURL fileURLWithPath:path]; [cell.imageView setImageWithUrl:url]; –

回答

0

儘量不要設置成功塊零和使用此代碼:

[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 
      }]; 
+0

它的工作原理是如果我使用網址而不是本地網址。 – user3164474

+0

再次考慮後,如果圖像是本地的,爲什麼你使用AFNetworking呢?只需將圖像放入單元格即可。 – Segev

+0

我正在尋找解決方案,因爲如果沒有AFNetworking,集合視圖會變得滯後。 – user3164474

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"]]; 

或使用塊,

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 
    }]; 
+0

它在這裏不起作用。 – user3164474

+0

什麼是錯誤? –

+0

無效的網址。我正在嘗試使用本地網址。 – user3164474