【发布时间】:2016-04-16 19:47:41
【问题描述】:
我在我的 iOS 应用中使用 Facebook v4 SDK。为了获取相关信息,我经常使用[FBSDKProfile currentProfile] 单例。但是,我还需要易于访问的个人资料图片,因此写了一个类别来处理这个问题。
这是头文件:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
@interface FBSDKProfile (ProfileImage)
+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler;
@property (nonatomic, strong, readonly) UIImage *profileImage;
@end
这是实现文件:
#import "FBSDKProfile+ProfileImage.h"
@interface FBSDKProfile()
@property (nonatomic, strong, readwrite) UIImage *profileImage;
@end
@implementation FBSDKProfile (ProfileImage)
+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler {
FBSDKProfile *currentProfile = [FBSDKProfile currentProfile];
NSString *userId = currentProfile.userID;
if (![userId isEqualToString:@""] && userId != Nil)
{
[self downloadFacebookProfileImageWithId:userId completionBlock:^(BOOL succeeded, UIImage *profileImage) {
currentProfile.profileImage = profileImage;
if (handler) { handler(succeeded); }
}];
} else
{
/* no user id */
if (handler) { handler(NO); }
}
}
+(void)downloadFacebookProfileImageWithId:(NSString *)profileId completionBlock:(void (^)(BOOL succeeded, UIImage *profileImage))completionBlock
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", profileId]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error)
{
UIImage *image = [[UIImage alloc] initWithData:data];
completionBlock(YES, image);
} else{
completionBlock(NO, nil);
}
}];
}
@end
但是,我遇到了这个异常:
由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[FBSDKProfile setProfileImage:]: unrecognized selector sent to instance
这是为什么?
【问题讨论】:
-
我想你是在下载完成之前设置图像。
-
属性不会在类别中自动合成。您必须编写自己的 getter 和 setter,并为图像提供自己的存储空间。
-
@dan 你能详细说明一下吗?尝试合成它会显示此错误:@synthesize not allowed in a category's implementation
标签: ios objective-c facebook facebook-sdk-4.0 objective-c-category