2013-02-16 132 views
1

我試圖獲得響應圖像的網址,所以我可以將它作爲背景附加。它的作品,但它爲每一個附加相同的圖像。這是我的代碼:Facebook相冊圖片網址

getUserAlbums : function(){ 
    FB.api('/me/?fields=id,name,albums', function(response) { 
     var albums = response.albums.data; 
     var frame = ""; 
     console.log(albums); 
     _.each(albums, function(album) { 

      FB.api('/'+album.cover_photo+"/picture?type=album", function(response) { 
       var url = response.data; 
       _.each(url, function(bg) { 
        console.log(url); 
        $("li.album").css({backgroundImage:"url("+bg.replace("_s.jpg", "_a.jpg")+")"}); 
       }); 
      }); 

      frame += '<li class="album"><img src="images/fb_frame.png"/></li>'; 

      $('ul#albums').append(frame); 

     }); 

    }); 
} 

即時通訊使用下劃線btw這是什麼「_.each」是。 這裏真正的問題是,當附加照片時,它顯示每張專輯的相同照片,這意味着我的循環被覆蓋,但我不明白爲什麼。

回答

0

我想通了,我不得不改變我在我的附加數據的方式:你不需要更換_s.jpg到a_jpg

FB.api('me/?fields=albums', function(response) { 
     var albums = response.albums.data; 
     _.each(albums, function(album, i) { 
      FB.api('/'+album.cover_photo+"/picture?type=album", function(res) { 
       var frame = $('<li class="album"><img src="images/fb_frame.png"/></li>').appendTo('ul#albums'); 
       frame.css({backgroundImage:'url('+res.data.url.replace('_s.jpg', '_a.jpg')+')'}); 
      }); 
     }); 
    }); 
0

。檢出照片對象的圖像字段。從這裏你可以選擇圖片。

me/?fields=id,name,albums.fields(photos.fields(images)) 

"albums": { 
    "data": [ 
     { 
     "id": "xxxxxx", 
     "created_time": "2011-04-01T03:17:15+0000", 
     "photos": { 
      "data": [ 
      { 
       "images": [ 
       { 
        "height": 1107, 
        "width": 2048, 
        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/s2048x2048/xxxx_n.jpg" 
       }, 
       { 
        "height": 330, 
        "width": 610, 
        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/XXXX_n.jpg" 
       }, 
       { 
        "height": 330, 
        "width": 610, 
        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/xxxxx_n.jpg" 
       }, 
       { 
        "height": 324, 
        "width": 600, 
        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/s600x600/xxxxxxxxx_n.jpg" 
       }, 
       { 
        "height": 259, 
        "width": 480, 
        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/s480x480/xxxxxxxx_n.jpg" 
       }, 
       { 
        "height": 173, 
        "width": 320, 
        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/s320x320/xxxxxxx_n.jpg" 
       }, 
       { 
        "height": 97, 
        "width": 180, 
        "source": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-prn1/xxxxxx_a.jpg" 
       }, 
       { 
        "height": 70, 
        "width": 130, 
        "source": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-prn1/xxxxxxxx_s.jpg" 
       }, 
       { 
        "height": 70, 
        "width": 130, 
        "source": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-prn1/s75x225/xxxxxx_s.jpg" 
       } 
       ], 
       "id": "4274066575883", 
       "created_time": "2013-02-09T10:43:54+0000" 
      }, 
相關問題