我正在寻找一个清晰而完整的解释,以获取一组Facebook好友(其Facebook ID存储在一个数组中)的个人资料图片的最佳方式。在我的应用程序的其他地方,我使用以下代码来获取当前用户的图像:
...
...
// Create request for user's Facebook data
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"no fb error");
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
// Download the user's facebook profile picture
self.imageData = [[NSMutableData alloc] init]; // the data will be loaded in here
// URL should point to https://graph.facebook.com/{facebookId}/picture?type=large&return_ssl_resources=1
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=square&return_ssl_resources=1", userData[@"id"]]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:pictureURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:2.0f];
// Run network request asynchronously
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (!urlConnection) {
NSLog(@"Failed to download picture");
}
}
}];
}
// Called every time a chunk of the data is received
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.imageData appendData:data]; // Build the image
}
// Called when the entire image is finished downloading
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[PFUser currentUser] setObject:self.imageData forKey:@"image"];
[[PFUser currentUser] saveInBackground];
}
这种方法似乎具有在后台下载数据的优势,但似乎将它作为for
循环的一部分实现它可能很棘手,我循环通过一组用户的Facebook朋友们得到他们的个人资料图片,因为它调用了多个委托方法,然后我必须跟踪委托方法中正在接收哪个朋友的图像数据。
甚至可以跟踪在委托方法中接收哪个朋友的图像数据,因为迭代通过这些朋友的for循环将在主线程上执行并在数据收集完成之前完成以用于先前的迭代(我相信)?
我想解决这个问题的一种方法是在后台线程中执行整个for循环,我不会迭代到下一个朋友,直到当前收集其所有数据。然后我不需要使用NSURLConnection
异步调用。这可能会简化代码。这是一个很好的解决方案,如果是这样,我将如何设置这样的后台流程?
或者你会推荐其他什么解决方案?
答案 0 :(得分:1)
使用NSURLConns方法:+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
,如下所示:
// Create request for user's Facebook data
FBRequest *request = [FBRequest requestForMe];
// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"no fb error");
// result is a dictionary with the user's Facebook data
NSDictionary *userData = (NSDictionary *)result;
// Download the user's facebook profile picture
self.imageData = [[NSMutableData alloc] init]; // the data will be loaded in here
// URL should point to https://graph.facebook.com/{facebookId}/picture?type=large&return_ssl_resources=1
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=square&return_ssl_resources=1", userData[@"id"]]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:pictureURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:2.0f];
// Run network request asynchronously
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *data, NSError *error) {
[[PFUser currentUser] setObject:data forKey:@"image"];
[[PFUser currentUser] saveInBackground];
}];
}
}];
}
答案 1 :(得分:0)