View user profile Image from Salesforce in iOS app

I am trying to display a registered user profile image using the 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 the image in a UIImageView.

Any help would be appreciated.

+3
source share
4 answers

Call the Salesforce Rest API service and retrieve 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 a json response. - (void) request: (SFRestRequest *) request didLoadResponse: (id) dataResponse {....}

fullEmailPhotoUrl / . , , . . :

= {

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

};
0

API Chatter - "smallPhotoUrl". , URL .

: Chatter

, : salesforce_platform_mobile_services.pdf 262

, :

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


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

, , , . , .

!

+2

Apple , dataWithContentsOfURL , .

, - , :

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

( . .)

In your case, you can (for example) save the image in CoreData or assign the NSData UIImage in this way:

[[UIImage alloc] initWithData: picData];

You also need to make sure your UIImage is properly connected, for example. One way would be through a UIImageView that is an IBOutlet.

+1
source

In fast version 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 {
                print(response.result)
                // Show the downloaded image:
                if let data = response.data {
                    debugPrint(data)
                    self.profileImg.image = UIImage(data: data)
                    self.profileImg.contentMode = .scaleAspectFill
                    debugPrint(UIImage(data: data)!)
                }
            }
          }
        }
0
source

All Articles