3

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 .

neo
  • 169
  • 2
  • 10
  • 1
    What have your tried and what have you eliminated as possible causes of problems. Have you checked pictureUrl is valid? If not is idData valid? Does [SFAccountManager sharedInstance].idData.pictureURL return a valid URL. Does the call to dataWithContentsOfURL reutrn any data? Why have you not written information regarding these aspects in your question? If you haven't tried them why are you posting before trying anything for yourself first? – Gruntcakes Feb 01 '14 at 00:03
  • I did test all u mention. To verify the url i even pasted the link on safari and gives me picture after login into salesforce. I have added the code snippet along. [profilePicData writeToFile:filePath atomically:YES]; It does writes picture file on local but when open shows invalid file. – neo Feb 03 '14 at 17:57
  • You haven't shown any code associated with loading/displaying it in the UIImageView. – Gruntcakes Feb 03 '14 at 23:24
  • figure it out. No problem with loading/displaying in UIImageview. Turns out you have to use "fullEmailPhotoUrl" instead of idData.pictureUrl. – neo Feb 04 '14 at 17:37

4 Answers4

2

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!

Justas
  • 136
  • 7
1

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.

Bradley Thomas
  • 4,060
  • 6
  • 33
  • 55
0

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";

};
neo
  • 169
  • 2
  • 10
0

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)
                }
            }
          }
        }
kdhingra7
  • 43
  • 6