我想使用我的AppID在Facebook上分享我的图像。所以我可以在facebook上看到via App
。它工作正常,直到iOS 5.但不是在iOS 6中。
如果我正在使用SLComposeViewController
,那么我可以分享图片,但是会通过iOS'而不是'通过应用'显示。所以我已经审查了它的代码并应用了,但它没有共享图像。如有任何问题,请检查代码并更正。
facebookaccount = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = @{ ACFacebookAppIdKey: @"xxxxxxxxxxxxx", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accountsArray = [facebookaccount accountsWithAccountType:facebookaccountType];
facebookAccount = [[accountsArray lastObject] retain];
NSLog(@" Account :::: %@", facebookAccount);
} else {
NSLog(@" Error :::: %@", error.description);
}}];
NSDictionary *parameters = @{@"message" : @""};
NSString *link = [NSString stringWithFormat:@"%simages/%@",ServerPath,[images objectAtIndex:imageNO]];
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];
CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression){
compression -= 0.1;
imageData = UIImageJPEGRepresentation(img1, compression);
}
UIImage *img = [[UIImage alloc] initWithData:imageData];
NSLog(@"Image ::: %@", img);
NSData *myImageData = UIImagePNGRepresentation(img);
NSLog(@" Image Data ::: %@", myImageData);
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:requestURL parameters:parameters];
[facebookRequest addMultipartData:myImageData withName:@"source" type:@"multipart/form-data" filename:nil];
NSLog(@"Request Account :::: %@", facebookAccount);
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Done with Facebook.......");
if (!error) {
NSDictionary *list = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@" Dictionary ::: %@", list);
} else {
NSLog(@" Error :::: %@", error.description);
}}];
我在字典日志中得到空值。
可能是什么问题?谢谢。
答案 0 :(得分:5)
使用下面的代码而不是SLComposeViewController使用SLRequest
NSDictionary *parameters = @{@"message": sendmessage};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
parameters:parameters];
[facebookRequest addMultipartData: myImageData
withName:@"source"
type:@"multipart/form-data"
filename:@"TestImage"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];
答案 1 :(得分:1)
我希望这也会对你有所帮助
-(void)facebookPost:(NSString *)text{
ACAccountStore *facebookaccount1 = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount1 accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = @{ ACFacebookAppIdKey: @"xxxxxxxxxxxxxxx", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount1 requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [facebookaccount1 accountsWithAccountType:facebookaccountType];
if ([accountsArray count] > 0)
{
NSDictionary *parameters = @{@"message": text};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
parameters:parameters];
NSString *link = [NSString stringWithFormat:@"%simages/%@",ServerPath,[images objectAtIndex:imageNO]]; // image link
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];
// code for compress image you can delete this code if you dont want to compress image
CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1;
imageData = UIImageJPEGRepresentation(img1, compression);
}
// imageData is your image
[facebookRequest addMultipartData: imageData
withName:@"source"
type:@"multipart/form-data"
filename:@"TestImage"];
ACAccount *facebookAccount1 = [accountsArray objectAtIndex:0];
[facebookRequest setAccount:facebookAccount1];
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];
}
}
}];
}