2016-04-14 40 views
0

我使用下面的URL來設置我的imageView(位於路徑中)。是的,URL數據成功返回,但由於某種原因,我imageView保持空白(只是白色?)imageView設置爲URL時空白?

是的,我已經連接了我的imageView屬性...任何想法有什麼不對?我應該使用不同的代碼塊來完成這個任務嗎?

Viewcontroller.m

NSMutableDictionary *viewParamsDogs = [NSMutableDictionary new]; 
    [viewParamsDogs setValue:@"mydogs" forKey:@"view_name"]; 
    [DIOSView viewGet:viewParamsDogs success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     self.dogData = [responseObject mutableCopy]; 

     [operation responseString]; 


     NSDictionary *dic = [responseObject valueForKey: @"field_pet_photo_path"]; 
          NSArray *arr = [dic valueForKey: @"und"]; 
          NSDictionary *dic2= [arr objectAtIndex : 0]; 
     NSString *path = [NSString stringWithFormat:@"%@", [dic2 valueForKey: @"safe_value"]]; 

     NSLog(@"This is path %@", path); 



    if([path length]>0) { 


     NSURL *url = [NSURL URLWithString:path]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *image = [UIImage imageWithData:data]; 
     self.dogimageView.image = image; 


    } else { 

      NSString *ImageURL = @"http://url.ca/paw.png"; 
      NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; 
      self.dogimageView.image = [UIImage imageWithData:imageData]; 
     } 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Failure: %@", [error localizedDescription]); 
    }]; 

路徑:

[5111:1673330] This is path (
    "http://url.com/default/files/stored/1460659054.jpg" 
) 

的NSLog(@ 「這是路徑%@,%@」,路徑,NSStringFromClass(path.class) );回報:

2016-04-14 13:18:39.590 [5225:1700657] This is path (
    "http://url.com/default/files/stored/1460659054.jpg" 
), __NSCFString 
+0

URL是服務器還是文檔目錄? – Lion

+0

@Lion服務器我相信? –

+0

imageData中是否有任何數據?如果不是,那麼檢查你的網址是否有任何東西 –

回答

0

我建議所有的網絡連接功能AFNetworking https://github.com/AFNetworking/AFNetworking的使用。

  1. 導入UIImageView + AFNetworking。在文件的開頭h,從DIC2

    NSString *path = dic2[@"safe_value"];

  2. 檢查路徑是一個字符串

    #import "UIImageView+AFNetworking.h"

  3. 獲取路徑

    if (![path isKindOfClass:[NSString class]]) { return; }

  4. 創建從NSURL對象ST環

    NSURL *imageUrl = [NSURL URLWithString:path];

  5. 設置imageUrlUIImageView

    [self.dogimageView setImageWithURL:imageUrl];

你可以在我的要點檢查全碼https://gist.github.com/MaciejGad/fa8cb80dfc73ea55edeedb75418ea2ec


編輯: 錯誤的原因是,被設置爲路徑:

NSString *path = @"(
\"http://url.com/default/files/stored/1460659054.jpg\" 
)"; 

更改爲正常的URL,我們需要修剪:此微調路徑之後

NSMutableCharacterSet *characterSetToTrim = [NSMutableCharacterSet characterSetWithCharactersInString:@"()\""]; 
[characterSetToTrim formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
path = [path stringByTrimmingCharactersInSet:characterSetToTrim]; 

應該http://url.com/default/files/stored/1460659054.jpg

+0

試過這個 - 我已經在使用AFNetworking,所以它很容易實現。但是這行[self.dogimageView sd_setImageWithURL:imageUrl]; ....正在崩潰我的應用程序,出現以下錯誤:' - [UIImageView sd_setImageWithURL:]:無法識別的選擇器發送到實例0x12bff86b0' –

+0

'AFNetworking'使用名稱'setImageWithURL'而不是'sd_setImageWithURL'。檢查您是否正確安裝了庫。最好的方法是使用Cocoa Pods https://github.com/AFNetworking/AFNetworking#installation-with-cocoapods。要了解有關可口豆莢的更多信息,請訪問https://cocoapods.org/方法'sd_setImageWithURL'來自https://github.com/rs/SDWebImage – bazyl87

+0

Ahh good catch!我正在使用AFNetworking以及SDWebImage - 因此混亂ahaha。應用程序不再崩潰,但我的imageUrl仍然爲空(登錄此行)? NSURL * imageUrl = [NSURL URLWithString:path]; –

0

嘗試這樣,

NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:data]; 
self.dogimageView.image = [[UIImageView alloc] initWithImage:image]; 

,並可以緩存圖像使用大圖書館SDWebImage。當圖像來自服務器使用這個庫是非常有幫助的。希望這會起作用。

更新:

NSString *path = [NSString stringWithFormat:@"%@", [dic2 valueForKey: @"safe_value"]]; 
NSURL *url = [NSURL URLWithString:path]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *image = [UIImage imageWithData:data]; 
self.dogimageView.image = [[UIImageView alloc] initWithImage:image]; 

試試這個。 :)

+0

url應該是第一行的路徑,不是嗎?但是當我放入路徑時,我得到不兼容的指針類型? –

+0

哪種路徑?iphone的文檔目錄的路徑? – Lion

+0

查看上面的代碼編輯(什麼路徑) –

0

嘗試使用dataWithContentsOfURL:選項:錯誤:不是,看看是否有任何錯誤信息有

+0

記錄每一行 - 出於某種原因ImageURL是路徑變成空值的地方,因此imageData是空的。我只是不確定爲什麼ImageURL在路徑明顯填充時返回null? –

+0

您的實際網址是?字符串......它可能有特殊字符。嘗試使用:[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; –