2012-10-18 33 views
0

我試圖從UIImageView中的URL顯示圖像,我看到一些非常奇怪的結果。的代碼,我與測試位低於iOS UIImageView dataWithContentsOfURL返回空

imageURL = @"http://images.shopow.co.uk/image/user_dyn/1073/32/32"; 
imageURL = @"http://images.shopow.co.uk/assets/profile_images/default/32_32/avatar-male-01.jpg"; 

NSURL *imageURLRes = [NSURL URLWithString:imageURL]; 
NSData *imageData = [NSData dataWithContentsOfURL:imageURLRes]; 
UIImage *image = [UIImage imageWithData:imageData]; 

NSLog(@"Image Data: %@", imageData); 

在它的當前形式,我可以看到這是我所期望的輸出窗口數據。然而,如果註釋掉第二個imageURL,所以我引用第一個我得到空數據,因此nil被imageWithData返回。可能更令人困惑的是,第一個圖像與第二個圖像基本相同,但它是通過PHP處理腳本完成的。我幾乎可以肯定,這是不是造成問題的劇本,因爲如果我用這個代替

imageURL = @"http://images.shopow.co.uk/image/product_dynimg/389620/32/32" 

顯示圖像,這使用相同的圖像處理腳本。我努力在圖像中找到任何會導致這種情況發生的差異。任何幫助,將不勝感激。

+0

如果您將擴展名添加到URL(如http://images.shopow.co.uk/image/user_dyn/633/32/32.png) – deanWombourne

+0

然後嘗試查看它是否爲'URLWithString'這是返回'零'? – deanWombourne

+0

它看起來像直接的URL不像其他人那樣使用Gzip。你可能想看看[AFNetworking](https://github.com/AFNetworking/AFNetworking),因爲他們似乎在處理GZip。 – rckoenes

回答

0

這是服務器配置的東西。

$ curl -v http://images.shopow.co.uk/assets/profile_images/default/32_32/avatar-male-01.jpg 

返回非零Content-Length和實際圖像的比特。當我做:

$ curl -v http://images.shopow.co.uk/image/user_dyn/633/32/32 

我碰到下面的標題和長度爲零的響應:

> GET http://images.shopow.co.uk/image/user_dyn/633/32/32 HTTP/1.1 
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 
> Host: images.shopow.co.uk 
> Accept: */* 
> Proxy-Connection: Keep-Alive 
> 
< HTTP/1.1 200 OK 
< Date: Thu, 18 Oct 2012 10:51:31 GMT 
< Server: Apache/2.2.3 (Red Hat) 
< X-Powered-By: PHP/5.3.15 
< Vary: Accept-Encoding,User-Agent 
< Content-Type: text/html; charset=UTF-8 
< Content-Length: 0 
< Proxy-Connection: Keep-Alive 
< Connection: Keep-Alive 

令人驚訝的在瀏覽器既聯繫工作,並返回一個圖像。

更新:我想通了。這是用戶代理。試試看這個:

$ curl -v \ 
    -A "User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4" \ 
    http://images.shopow.co.uk/image/user_dyn/633/32/32 

你會得到你的圖像位。

+0

謝謝,它原來是爲服務圖像的應用程序的配置設置。這取決於請求來自何處,並且您對用戶代理的建議使我走上了正確的軌道。 – olliefinn

相關問題