Cache Facebook Profile Picture iOS

大兔子大兔子 提交于 2019-12-12 19:19:33

问题


I'm saving images to disk for my app users, who either upload a picture or connect with facebook and we display their profile picture.

This works great, but if a user changes their facebook profile picture, the code below never updates the image.

How can I check the facebook graph api to check if the image has been uploaded without redownloading the image? I am using AFNetworking so checking the 302 header is an option if its possible-just not sure how to implement.

//load image if exists on disk

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:uniqueCustomerPath];
    BOOL fileExists = [fileManager fileExistsAtPath:filePath];
    if(fileExists){
        UIImage* tempImage = [UIImage imageWithContentsOfFile:filePath];
        completionHandler(tempImage);
    }

//download image if not
    else{                
            NSString *imageUrl=[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", (long)self.customer.facebook_id];
            UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
            NSData *myData = UIImageJPEGRepresentation(tempImage, 1);                
            [myData writeToFile:filePath atomically:YES];
            completionHandler(tempImage);
        }

来源:https://stackoverflow.com/questions/29811388/cache-facebook-profile-picture-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!