2012-03-19 69 views
0

我試圖從Vimeo獲取視頻縮略圖,但由於某種原因,我無法將返回的NSData轉換爲UIImage。這是我的代碼。我該如何將類型的NSData變成UIImageiPhone iOS從Vimeo獲取縮略圖:如何將響應轉換爲圖像?

-(void)getThumbnails 
{ 
    int count = _videos.count; 
    Video* v = nil; 
    RKRequest* r = nil; 

    for(int i = 0; i<count; i++) 
    { 
     v = [_videos objectAtIndex:i]; 


     r = [[RKClient sharedClient] get:v.thumbnail delegate:self]; 
     GMGridViewCell* gridViewCell = [self GMGridView:_gmGridView cellForItemAtIndex:i]; 
//prints http://b.vimeocdn.com/ts/257/009/257009714_200.jpg 
     NSLog(@"thumbnail url: %@",v.thumbnail); 

     [r setUserData:gridViewCell]; 

    } 

} 

這裏的響應:

-(void)requestQueue:(RKRequestQueue *)queue didLoadResponse:(RKResponse *)response 
{ 

    if([response.request.userData isKindOfClass:[GMGridViewCell class]]) 
    { 


     //prints content type: text/html; charset=UTF-8 
     NSLog(@"content type: %@",response.contentType); 

     GMGridViewCell* cell = response.request.userData; 

     NSData* responseBody = response.body; 
     UIImage* image = [UIImage imageWithData:responseBody]; 

     cell.imageView.image = image; 

      } 
} 

更新:

檢查響應爲體內後,似乎VIMEO返回找不到網頁,在鍵入時即到瀏覽器返回一個圖像:/

回答

0

我想出了答案:

r = [[RKClient sharedClient] get:v.thumbnail delegate:self]; 

在前面加上一個設置了當客戶端在請求前被初始化默認路徑,創建一個http:// ... HTTP:// ...就像服務器無法識別的路徑,用未找到的消息回覆。

0

雖然我不能幫你解決主要問題,但我可以爲你澄清,如果你不知道, NSData本質上只是一個字節緩衝區,所以要使用[UIImage imageWithData:]消息,您需要NSData來包含適當的圖像文件(jpg,png等)。

如果你的回答不是純粹的圖像,而是其他的東西,你需要解析你的NSData來提取你需要的圖像數據。

我希望你能走上正軌。