我在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
但是,我得到了这个例外:
由于未捕获的异常终止应用&#39; NSInvalidArgumentException&#39;,原因:&#39; - [FBSDKProfile setProfileImage:]:无法识别的选择器已发送到实例
为什么会这样?
答案 0 :(得分:0)
你可以使用,
public ContactViewModel(ObservableCollection<Room> room)
{
// Initialize dropdown data for titlelist
DropDownTitle = GenerateDropDownDataForTitle();
DropDownCountry = GenerateDropDownDataForCountry();
ContactModel = new ContactDetails(1, "", "", "", "", 1);
// Initialize commands
BookCommand = new RelayCommand(BookCommand_DoWork, () => true);
BackCommand = new RelayCommand(BackCommand_DoWork, () => true);
if (room != null)
{
SelectedRooms = room;
}
}
请参阅this link了解详情。
答案 1 :(得分:0)
<强>问题强>
您已将属性readonly
添加到您的媒体资源profileImage
。
它意味着什么:
你可以只读,所以调用setter会抛出异常。
<强>解决方案强>
不要将readonly
属性分配给profileImage
@property (nonatomic, strong) UIImage *profileImage;