簡體   English   中英

獲取facebook個人資料圖片

[英]get facebook profile picture

目前我想獲取facebook個人資料圖片然后將圖片轉換為CCSprite。

到目前為止我的代碼看起來像這樣:

//fbId is facebook id of someone
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal", fbId]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

//convert UIImage to CCSprite
CCTexture2D *texture = [[[CCTexture2D alloc] initWithImage:image resolutionType:kCCResolutionUnknown] retain];
CCSprite *sprite = [CCSprite spriteWithTexture:texture];
[self addChild:sprite];

它有效,但在加載前需要一段時間,大約幾秒鍾。

我的問題是,除了互聯網連接,有沒有更好的方法盡快加載Facebook個人資料圖像? 謝謝

您將網絡代碼放在主線程中,這將阻止UI並擁有糟糕的用戶體驗。 通常,你應該把這些東西放在另一個線程中。 嘗試使用派遣

dispatch_queue_t downloader = dispatch_queue_create("PicDownloader", NULL);
dispatch_async(downloader, ^{
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    dispatch_async(dispatch_get_main_queue(), ^{
        CCTexture2D *texture = [[[CCTexture2D alloc] initWithImage:image resolutionType:kCCResolutionUnknown] retain];
        CCSprite *sprite = [CCSprite spriteWithTexture:texture];
        [self addChild:sprite];
    });
});

或者如果您願意,可以嘗試JBAsyncImageView 也許你必須將其破解為你的CCSprite :)

首先,我會選擇使用AFNetworking異步請求圖像數據。 我的猜測是,這是導致延遲的原因。 您可能會注意到在幾秒鍾內UI被鎖定。 異步調用該數據將解決此問題。 更好的是,您可以考慮使用Facebook的iOS SDK來調用圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM