How to Convert Fbprofilepictureview to an Uiimage

How can I convert FBProfilePictureView to an UIImage?

FBProfilePictureView is a UIView, this UIView contains a UIImageView, that is your image, you can get the UIImage from that UIImageView:

profilePictureView is a FBProfilePictureView

UIImage *image = nil;

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

EDIT: add another way more quickly but do the same thing

__block UIImage *image = nil;

[self.view.subviews enumerateObjectsUsingBlock:^(NSObject *obj, NSUInteger idx, BOOL *stop) {
if ([obj isMemberOfClass:[UIImageView class]]) {
UIImageView *objImg = (UIImageView *)obj;
image = objImg.image;
*stop = YES;
}
}];

Get an image from FBProfilePictureView swift

you can use alternate Idea also,

Objective-C

 NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser objectID]];

change the types also

  • type: small, normal, large, square

Swift

 let avatar = "https://graph.facebook.com/\(user.objectID)/picture?width=640&height=640"

or another choice

let url = NSURL.URLWithString("https://graph.facebook.com/\(user.objectID)/picture?width=640&height=640");
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
yourimagename.image = UIImage(data: data!)

Turn FBProfilePictureView into UIImage

I would not use the image like this since facebook will load different sizes of that image and you don't know whether you will just get a low quality version. Can't you just download the image for yourself in the size you need it?

Facebook uses urls like this:
http://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT

ofc you need to swap out USER_ID for the user's facebook user id and WIDTH and HEIGHT to values you need.

How to get the FBProfile picture into UIImageView

If you have Facebook user.id with you, you can create the imageURL like this

   NSString * url = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large",user.objectID];

Then save this url to your singleton class. And you can download like this if you need.(You can download each time you required or download once and save in to a file, if required ,fetch from file).

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
UIImage *userImage = [UIImage imageWithData:data];

FBProfilePictureView - Save UIImage to UserDefaults

So this was a question I was looking for on here the other day but couldn't find a correct answer that worked so I thought I'd answer my own for the community. Here goes:

You will need to have already setup a login button with the:

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

delegate method already working.

We'll use a login screen with a 'Continue' button after loggin=Success to capture the UIImage, so add a "Continue" button (for pushing to the next screen after login) onto your storyboard and also a View with the class of "FBProfilePictureView" which is linked to the header file like so:

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

Then synthesise it in the .m file like so:

@synthesise userProfilePic;

Then set the Delegate in ViewDidLoad like so:

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

Now we want to add this line anywhere in the .m file (make sure it's not nested inside a function!)

id<FBGraphUser>cachedUser;

Inside the Delegate method we mentioned earlier (fbMethodLoggedInWithFbUser) we will set our newly created id tag to equal the passthrough value of the delegate method like so:

- (void)fbMethodLoggedInWithFbUser:(id<FBGraphUser>)user {
cachedUser = user;

// other login methods go here

}

Now your user is logged in, we have a cache of the '' id. The reason that this works best with a 'Continue' button after the user has logged in, is because the code I'm going to post will fetch the default blank profile picture image that Facebook uses as a temp image until the users profile picture loads. So to make sure this doesn't happen, first add in these 2 methods, then we will link the first up to the 'Continue' button action:

- (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];
}
}

}

This method is to save our captured UIImage from the 'userProfilePic' view to the UserDefaults:

- (void)saveImageWithUDWithImage:(UIImage *)tempImage {

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:UIImagePNGRepresentation(tempImage) forKey:@"userProfilePicture"];
[ud synchronize];

}

Now set up your continue button like so:

- (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...

}

Then to read the UIImage from the UserDefaults later on, use this method:

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

This can be called in any other class that you want to display the user profile picture in like so:

- (void)viewDidLoad {

[myWantingToBeProfilePicture setImage:[self loadProfilePicFromUserDefaults];

}

Sorry for code being allover the place, but I've explained it in a way that made it clear to me, I just hope it's clear to everyone else too! Feel free to edit it and make it better!

@Declanland

How to retrieve an image from fbprofilepictureview and store this image into database

https://graph.facebook.com/username/picture?redirect=true

Using this url you can get profile pic.

In Facebook Delegate loginViewFetchedUserInfo you can get the image url of User Profile pic

 - (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{

NSString *imageUrl=[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?redirect=true", user.username];

}

how to save FBProfilePictureView in NSUserDefaults\Core Data?

You're talking about image caching - keep in mind that you can't just infinitely store images as they are downloaded (there is a chance that your application will take up way too much disk space eventually). You need to define some criteria for purging all of this data - either data that is unused or maybe outdated - that's up to you. There are a lot of ways to tackle the caching issue, just Google it and I'm sure you'll find some useful stuff.

As for storing images - what you want to do is get the UIImage's data (by using functions like UIImageJpegRepresentation or UIImagePNGRepresentation). These functions will return an NSData instance that can either be stored in NSUserDefaults (not the place for image caching) or on a file in your Cache directory (much better and won't give Apple a good reason to reject your app for bad storage practices).

Also, don't accidentally try to call these functions on an FBProfilePictureView instance - that's just a class Facebook created for displaying profile pictures (it is in fact a UIView that has a UIImageView subview of a profile image. Within this UIImageView you can get the UIImage itself). How can I convert FBProfilePictureView to an UIImage?

Good luck

how to hide FBProfilepictureview

FBProfilepictureview is subClass of UIView and its also support hide property of UIView so just set it hide on your button click
if you have object like this

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

then set like this

useProfileImage.hidden = YES;

or you can do like

useProfileImage.frame = CGRectZero

FBProfilePictureView not filling the assigned space in the storyboard

The problem is that FBProfilePictureView contains a UIImageView and does not expose it for you to modify it's properties.

It downloads a square image if pictureCropping == FBProfilePictureCroppingSquare otherwise it downloads a small, medium or large image depending on the width of the frame. But there is no way to control how this image is scaled to fill the FBProfilePictureView.

I created a replacement view which exposes the UIImageView through a read-only imageView property and therefore allows you to control its contentMode.



Related Topics



Leave a reply



Submit