获取Facebook个人资料专辑,无需遍历每个个人资料专辑

时间:2015-06-03 20:41:19

标签: facebook facebook-graph-api

function getAlbumCovers() {
    //first check to see if profile pics already exist in user model

    //create a collection
    var usersCollection = Alloy.Collections.user;

    facebookModule.requestWithGraphPath('me/albums', {
        fields : 'id,name,cover_photo,count,created_time'
    }, 'GET', function(graphResp) {
        //show view indicator before data is fetched
        //$.view_indicator.visible = true;
        setViewIndicator(100, 100, levelColour, 10, true, '30%');
        $.ind.top = '30';
        if (graphResp.success) {
            if (graphResp.result) {
                var rows = [];
                var data = JSON.parse(graphResp.result).data;
                var i = 0;
                var blob;

                for (x in data) {
                    if (data[x].name == "Profile Pictures") {
                        Ti.API.debug(JSON.stringify(data[x]));

                        var numberPhotos = null;
                        var albumTitle = null;

                        //apply transformations

                        if (data[x].count == null) {
                            numberPhotos = 0;

                        } else {

                            numberPhotos = data[x].count;
                            if (numberPhotos > 10) {
                                numberPhotos = 10;

                            } else {

                                numberPhotos = numberPhotos;
                            }
                        }

                        albumTitle = data[x].name + ' (' + numberPhotos + ')';
                        console.log("https://graph.facebook.com/" + (data[x].cover_photo || 0) + "/picture?access_token=" + Alloy.Globals.facebookModule.accessToken + "&type=album");
                        //start
                        // Read the image as a blob
                        var url = "https://graph.facebook.com/" + (data[x].cover_photo || 0) + "/picture?access_token=" + Alloy.Globals.facebookModule.accessToken + "&type=album";

                        var imgView = Ti.UI.createImageView({
                            image : url,
                            width : 'auto',
                            height : 'auto'
                        });
                        var imgBlob = imgView.toBlob();
                        var height = imgBlob.height;
                        var width = imgBlob.width;

                        if (height > width) {
                            var pos = (height - width) / 2;
                            var dict = {
                                height : width,
                                width : width,
                                x : 0,
                                y : pos
                            };
                            var newImg = imgBlob.imageAsCropped(dict);
                            //alert(newImg.height);
                        } else if (height < width) {
                            var pos = (width - height) / 2;
                            var dict = {
                                height : height,
                                width : height,
                                x : pos,
                                y : 0
                            };
                            var newImg = imgBlob.imageAsCropped(dict);
                            //alert(newImg.width);
                        } else {
                            //alert(imgBlob.width + '-' + imgBlob.height);
                            var pos = (width - height) / 2;
                            var dict = {
                                height : height,
                                width : height,
                                x : pos,
                                y : 0
                            };
                            var newImg = imgBlob.imageAsCropped(dict);
                        }

                        imgBlob = null;

                        //end
                        var row = Titanium.UI.createTableViewRow({
                            titleAlb : data[x].name,
                            selectedBackgroundColor : 'transparent',
                            selectionStyle : 'Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE on createTableViewRow',
                            width : '100%',
                            height : '60'
                        });
                        var image = Titanium.UI.createImageView({
                            //image : "https://graph.facebook.com/" + (data[x].cover_photo || 0) + "/picture?access_token=" + Alloy.Globals.facebookModule.accessToken + "&type=album",
                            image : newImg,
                            top : 10,
                            borderColor : levelColour,
                            left : 30,
                            width : '40',
                            height : '40'
                        });

                        if (Alloy.Globals.Platform == 'old') {
                            var title = Titanium.UI.createLabel({
                                text : albumTitle,
                                font : {
                                    visible : true,
                                    color : '#888888',
                                    fontFamily : 'Avenir-Roman',
                                    size : 12,
                                },
                                width : 200,
                                top : 15,
                                color : levelColour,
                                right : '30dp',
                                height : 'auto'
                            });
                        } else if (Alloy.Globals.Platform == '6') {
                            var title = Titanium.UI.createLabel({
                                text : albumTitle,
                                font : {
                                    visible : true,
                                    color : '#888888',
                                    fontFamily : 'Avenir-Roman',
                                    size : 12,
                                },
                                width : 200,
                                top : 15,
                                color : levelColour,
                                right : '85dp',
                                height : 'auto'
                            });

                        } else if (Alloy.Globals.Platform == '6+') {
                            var title = Titanium.UI.createLabel({
                                text : albumTitle,
                                font : {
                                    visible : true,
                                    color : '#888888',
                                    fontFamily : 'Avenir-Roman',
                                    size : 12,
                                },
                                width : 200,
                                top : 15,
                                color : levelColour,
                                right : '105dp',
                                height : 'auto'
                            });
                        }

                        row.add(image);
                        row.add(title);
                        rows.push(row);

                        i++;
                        console.log(i);

                        //set table rows
                        $.tableview.setData(rows);

                        //end
                        $.view_indicator.visible = false;
                        break;

                    }
                }

            }

        } else {
            $.view_indicator.visible = false;
            if (e.error) {
                alert(e.error);
            } else {
                alert("Unkown result");
            }
        }
    });

}

这是我的代码,你可以看到我正在调用Facebook api图表迭代每张专辑,然后检测个人资料专辑。这种方法的问题在于,如果用户有1000张专辑,那么for循环可以永远找到个人资料专辑。

任何人都知道更快的方法吗?

干杯

1 个答案:

答案 0 :(得分:0)

目前,Graph API中没有此类机制。

另外,你提出的方法充其量是不可靠的,因为不是每个人都使用英语中的Facebook,而对于那些不用的人来说,这张专辑可能不会被称为&#34; Profile Pictures&#34;。 / p>

获取用户当前个人资料照片(没有相册)的最佳方法是获取/user/picture