gpt4 book ai didi

ios - 无法检索 friend 的个人资料图片并将其显示在 tableview iOS Objective-C 中

转载 作者:行者123 更新时间:2023-11-29 00:30:14 26 4
gpt4 key购买 nike

我正在尝试在表格 View 中显示用户 facebook 好友的个人资料图片。我已经设法检索到 friend 的名字,但我很难获取 friend 的个人资料图片。

这是我的代码:

- (void)viewDidLoad {

if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"/me" parameters:@{@"fields":@"name"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@", result);
}
}];
}
FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_topProfilePic.frame];
//[profilePictureview setProfileID:result[@"id"]];

[_topProfilePic addSubview:profilePictureview];

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_topProfilePic.bounds
byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(1000.0, 1000.0)];

// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = _topProfilePic.bounds;
maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
_topProfilePic.layer.mask = maskLayer;

[super viewDidLoad];

}


-(void)getFriends {

FBSDKGraphRequest *requestFriends = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"/me/friends"
parameters:@{@"fields" : @"name,picture.type(small)"}
HTTPMethod:@"GET"];
[requestFriends startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (!error && result)
{
NSArray *allFriendsResultData = [result objectForKey:@"data"];

if ([allFriendsResultData count] > 0)
{
for (NSDictionary *friendObject in allFriendsResultData)
{
NSString *friendName = [friendObject objectForKey:@"name"];
NSString *friendID = [friendObject objectForKey:@"id"];
NSString *friendPhoto = [friendObject objectForKey:@"picture.type(small)"];

CFriend *newFriend = [CFriend addFriendWithName:friendName FriendID:friendID friendPhoto:friendPhoto];

if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects addObject:newFriend];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_objects.count-1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView reloadData];
}
}
}
}];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}

-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return @"VEXI";
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

CFriend *objects = self.objects[indexPath.row];
UILabel *overskriftlbl = (UILabel*) [cell viewWithTag:201];
overskriftlbl.text = [NSString stringWithFormat:@"%@", [objects friendName]];

UIImageView *thumbnailImageView = (UIImageView* )[cell viewWithTag:202];

thumbnailImageView.image = [UIImage imageNamed:[objects friendPhoto]];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:thumbnailImageView.bounds
byRoundingCorners:UIRectCornerAllCorners

cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = thumbnailImageView.bounds;
maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
thumbnailImageView.layer.mask = maskLayer;
return cell;
}

编辑:

我现在已经设法检索到图像的图形 URL。但是它们不会加载到 ImageView 中...请参见下面的代码:

    - (void)getFriends {

FBSDKGraphRequest *requestFriends = [[FBSDKGraphRequest alloc]
initWithGraphPath:@"/me/friends"
parameters:@{@"fields" : @"name,picture.type(small)"}
HTTPMethod:@"GET"];
[requestFriends startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (!error && result)
{
NSArray *allFriendsResultData = [result objectForKey:@"data"];



if ([allFriendsResultData count] > 0)
{
for (NSDictionary *friendObject in allFriendsResultData)
{
NSString *friendName = [friendObject objectForKey:@"name"];
NSString *friendID = [friendObject objectForKey:@"id"];
NSString *friendPhoto= [NSString stringWithFormat:@"graph.facebook.com/%@/picture?type(small)​", friendID];
CFriend *newFriend = [CFriend addFriendWithName:friendName FriendID:friendID friendPhoto:friendPhoto];

if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects addObject:newFriend];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_objects.count-1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
NSLog(@"%@", friendPhoto);
[self.tableView reloadData];
}
}
}
}];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return @"VEXI";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

CFriend *objects = self.objects[indexPath.row];
NSString *imageData = [NSString stringWithFormat:@"%@", [objects friendPhoto]];

UILabel *overskriftlbl = (UILabel*) [cell viewWithTag:201];
overskriftlbl.text = [NSString stringWithFormat:@"%@", [objects friendName]];

UIImageView *thumbnailImageView = (UIImageView*)[cell viewWithTag:202];
UIImage *profilePic = [UIImage imageNamed:imageData];
thumbnailImageView.image = profilePic;

//NSLog(@"%@", imageData);


UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:thumbnailImageView.bounds
byRoundingCorners:UIRectCornerAllCorners

cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = thumbnailImageView.bounds;
maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
thumbnailImageView.layer.mask = maskLayer;
return cell;
}

有什么想法吗?

最佳答案

这是我从 Facebook 检索所有 friend 的代码。您可以使用它创建自定义方法。

-(void)getFacebookFriend
{
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject:@"id, first_name, last_name, middle_name, name, email, picture" forKey:@"fields"];
[params setObject:[NSString stringWithFormat:@"500"] forKey:@"limit"];

[[FBRequest requestWithGraphPath:@"me/taggable_friends" parameters:params HTTPMethod:nil] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSArray *aryFriend = result[@"data"];
[yourFriendArrat addObjectsFromArray:aryFriend];
NSLog(@"%lu",(unsigned long)aryFriend.count);
NSMutableDictionary *dicPaging = result[@"paging"];


NSLog(@"Final Array count: %lu",(unsigned long)arrayFriend.count);
[yourTable reloadData];
}];
}

您可以在cellForRow 方法中使用以下代码来下载图片。

NSDictionary *dic = yourArray[indexPath.row];
NSString *strURL = [NSString stringWithFormat:@"%@",dic[@"picture"][@"data"][@"url"]];

我成功地从 facebook 获取好友列表。希望对您有所帮助。

关于ios - 无法检索 friend 的个人资料图片并将其显示在 tableview iOS Objective-C 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42085923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com