Question

I am trying to display logged in user's profile picture using following:

    NSLog(@"url is : %@",[SFAccountManager sharedInstance].idData.pictureUrl);
    profilePicData=[NSData dataWithContentsOfURL:[SFAccountManager sharedInstance].idData.pictureUrl];
    if ( profilePicData )
    {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.jpg"];
        NSLog(@"pic path:  %@",filePath);
        [profilePicData writeToFile:filePath atomically:YES];
    }
    NSLog(@"pic data:  %@",profilePicData);


}

in NSLog("%@", [NSData dataWithContentsOfURL:[SFAccountManager sharedInstance].idData.pictureUrl]); shows some data but does not display picture in UIImageView.

Any Help would be appreciated .

Was it helpful?

Solution 3

Calling salesforce Rest API and getting user information .

    NSString *path=@"/services/data/v29.0/chatter/users/me";
    SFRestMethod method=0;
    SFRestRequest *request = [SFRestRequest requestWithMethod:method path:path queryParams:queryParams];
    [[SFRestAPI sharedInstance] send:request delegate:self];

this will return json response. - (void)request:(SFRestRequest *)request didLoadResponse:(id)dataResponse {….}

Parse required photo dictionary from it and use fullEmailPhotoUrl to load/save images. using largePhotoUrl and largePhotoUrl did not load picture for me. Eg. of photo dictionary parsed:

photo = {

fullEmailPhotoUrl = "https://na14.salesforce.com/ncsphoto/<SOMEIDENTIFIERS>";

largePhotoUrl = "https://c.na14.content.force.com/profilephoto/<SOMEIDENTIFIERS>/F";

photoVersionId = <PHOTOID>;

smallPhotoUrl = "https://c.na14.content.force.com/profilephoto/<SOMEIDENTIFIERS>/T";

standardEmailPhotoUrl = "https://na14.salesforce.com/ncsphoto/<SOMEIDENTIFIERS>";

url = "/services/data/v29.0/chatter/users/<SOMEIDENTIFIERS>/photo";

};

OTHER TIPS

I was attempting to do exactly the same inside the Chatter API - I wanted to load image from feed data under "smallPhotoUrl". What I have discovered is, that at the end of URL active token has to be added.

This source introduced me to concept: Accessing Chatter user pics

And I finally was able to find some clear information how to access that token here: salesforce_platform_mobile_services.pdf page 262

So eventually I did this:

    NSString *builtImageUrlWithToken = [NSString stringWithFormat:@"%@?oauth_token=%@",
 phototoUrl, [SFAccountManager sharedInstance].credentials.accessToken];


    NSURL *imageURL = [NSURL URLWithString:builtImageUrlWithToken];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

I think it's relevant to this topic, as I came across it while looking for this particular solution. I am sure it could be useful for others as well.

Thanks!

First Apple docs say not to use dataWithContentsOfURL for network calls, because it is synchronous.

So, you can do something like this instead to get the image:

SFIdentityData *idData = [SFAccountManager sharedInstance].idData;

if(idData != nil) {
    SFRestRequest *request = [[SFRestRequest alloc] init];
    request.endpoint = [idData.pictureUrl absoluteString];
    request.method = SFRestMethodGET;
    request.path = @"";

    [[SFRestAPI sharedInstance] send:request delegate:_delegate];
}

(This is async. You could use blocks instead of delegation.)

In your delegate you can (for example) store the image in CoreData or assign the NSData to a UIImage thus:

[[UIImage alloc] initWithData:picData];

You do also need to make sure that your UIImage is properly wired up, e.g. one way would be through a UIImageView that's an IBOutlet.

In swift 3, Using Alamofire:

       if let url = URL(string: baseURl + "/sobjects/Attachment/\(imageID)/Body"){

       let header = ["Authorization": "Your Auth Token"]

        Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: header).responseData { (response) in
            if response.error == nil {
                // Show the downloaded image:
                if let data = response.data {
                    self.profileImg.image = UIImage(data: data)
                }
            }
          }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top