【问题标题】:FBProfilePictureView - Save UIImage to UserDefaultsFBProfilePictureView - 将 UIImage 保存到 UserDefaults
【发布时间】:2014-07-21 15:02:41
【问题描述】:

如何从 FBProfilePictureView 中获取 UIImage?我找不到正确解释的帖子,因此请查看下面的答案并随时进行编辑!

【问题讨论】:

    标签: xcode facebook uiimage profile facebook-ios-sdk


    【解决方案1】:

    所以这是我前几天在这里寻找的一个问题,但找不到有效的正确答案,所以我想我会为社区回答我自己的问题。如下:

    您需要已经设置了一个登录按钮:

    - (void)fbMethodLoggedInWithFbUser:(id<FBGraphUser>)user
    

    委托方法已经在工作了。

    我们将在 loggin=Success 之后使用带有“继续”按钮的登录屏幕来捕获 UIImage,因此在您的故事板上添加一个“继续”按钮(用于在登录后推送到下一个屏幕)以及一个带有像这样链接到头文件的“FBProfilePictureView”类:

    @property (strong, nonatomic) IBOutlet FBProfilePictureView *userProfilePic;
    

    然后像这样在.m文件中合成它:

    @synthesise userProfilePic;
    

    然后像这样在 ViewDidLoad 中设置 Delegate:

    - (void)viewDidLoad {
        [userProfilePic setDelegate:self];
    }
    

    现在我们想在 .m 文件中的任意位置添加这一行(确保它没有嵌套在函数中!)

    id<FBGraphUser>cachedUser;
    

    在我们之前提到的委托方法(fbMethodLoggedInWithFbUser)中,我们将设置我们新创建的 id 标签等于委托方法的传递值,如下所示:

    - (void)fbMethodLoggedInWithFbUser:(id<FBGraphUser>)user {
        cachedUser = user;
    
        // other login methods go here
    
    }
    

    现在您的用户已登录,我们有一个 '' id 的缓存。在用户登录后使用“继续”按钮效果最好的原因是,我要发布的代码将获取 Facebook 用作临时图像的默认空白个人资料图片图像,直到用户个人资料图片加载.所以为了确保不会发生这种情况,首先添加这两个方法,然后我们将第一个链接到“继续”按钮操作:

    - (void)getProfilePictureWithFbUser:(id<FBGraphUser>)user {
        userProfilePic.profileID = user.id;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4f];
        [userProfilePic setAlpha:1];
        [UIView commitAnimations];
    
        // -----------------------
        // CATCH PROFILE PICTURE::
    
        for (id obj in userProfilePic.subviews) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *tempImageView = obj;
                UIImage *tempImage = tempImageView.image;
                [self saveImageToUDWithImage:tempImage];
            }
        }
    
    }
    

    这个方法是将我们从'userProfilePic'视图中捕获的UIImage保存到UserDefaults:

    - (void)saveImageWithUDWithImage:(UIImage *)tempImage {
    
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setObject:UIImagePNGRepresentation(tempImage) forKey:@"userProfilePicture"];
        [ud synchronize];
    
    }
    

    现在像这样设置您的继续按钮:

    - (IBAction)continueButtonActionAfterLogin:(id)sender {
    
        // First we capture the user profile pic
        // with the cached id we got earlier after
        // login:
    
        [self captureProfilePicWithFBUser:cachedUser];
    
        // You can execute model pushes here, etc...
    
    }
    

    然后稍后从 UserDefaults 中读取 UIImage,使用此方法:

    - (UIImage *)loadProfilePicFromUserDefaults {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSData *imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"userProfilePicture"];
        UIImage *image = [UIImage imageWithData:imageData];
        return image;
    }
    

    这可以在您想要显示用户个人资料图片的任何其他类中调用,如下所示:

    - (void)viewDidLoad {
    
        [myWantingToBeProfilePicture setImage:[self loadProfilePicFromUserDefaults];
    
    }
    

    很抱歉代码到处都是,但我已经以一种让我清楚的方式解释了它,我只是希望其他人也清楚!随意编辑它,让它变得更好!

    @Declanland

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2020-02-27
      • 2021-02-09
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      相关资源
      最近更新 更多