FBProfilePictureView加载延迟

时间:2014-01-13 07:18:01

标签: ios facebook-graph-api

我正在处理FBProfilePictureView。我尝试了以下方法,它的工作原理。

- (void)getUserImageFromFBView
{
UIImage *img = nil;

for (NSObject *obj in [self.profilePictureView subviews]) {
    if ([obj isMemberOfClass:[UIImageView class]]) {
        UIImageView *objImg = (UIImageView *)obj;
        img = objImg.image;
        break;
    }
}
}

在这里,我必须延迟调用这个方法,比如5.0f 在某些情况下,图像下载在此期间内未完成。 有没有办法检查facebook个人资料图片下载是否完成。当用户在Facebook中更改个人资料图片时,也会发生图像下载延迟

感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:4)

而不是尝试:

- (void) getFBImage
{
    NSString *facebookID = @"";

    if (![_resultDictionary objectForKey:@"username"] && ![[_resultDictionary objectForKey:@"username"] isEqualToString:@""])
    {
        facebookID = [_resultDictionary objectForKey:@"username"];
    }
    else if ([_resultDictionary objectForKey:@"id"] && ![[_resultDictionary objectForKey:@"id"] isEqualToString:@""])
    {
        facebookID = [_resultDictionary objectForKey:@"id"];
    }

    if ([facebookID isEqualToString:@""])
    {
        return;
    }
    else
    {
        NSString *string=[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=105&height=109",facebookID];

        NSURL *url = [NSURL URLWithString:string];

        dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);

        dispatch_async(downloadQueue, ^{

            NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];

            dispatch_async(dispatch_get_main_queue(), ^{

                [_imgDisplay setImage:[UIImage imageWithData:imgData]];

            });
        });
    }
}

// _ imgDisplay是我的UIImageView,显示Facebook个人资料图片,_resultDictionary包含Facebook用户ID或用户名。

答案 1 :(得分:0)

您可以使用像SDWebImage这样的图片下载框架,并使用该方法跟踪图片是否已下载

- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
                                         options:(SDWebImageDownloaderOptions)options
                                        progress:(SDWebImageDownloaderProgressBlock)progressBlock
                                       completed:(SDWebImageDownloaderCompletedBlock)completedBlock;