所以我正在创建一个iPhone应用程序,当用户使用Facebook Graph API检查位置时,需要能够上传照片。
现在我的代码是:
if (![delegate.facebook isSessionValid])
[delegate.facebook authorize:[NSArray arrayWithObjects:@"publish_stream", @"offline_access", @"publish_checkins", nil]];
parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:[placeDictionary objectForKey:@"id"] , @"place",
serialisedCoordinates, @"coordinates",
message, @"message",
pickedImage, @"picture",
@"Via the official REDACTED app", @"application", nil];
[delegate.facebook requestWithGraphPath:@"me/checkins" andParams:parameters andHttpMethod:@"POST" andDelegate:fbCheckInResultHandler];
其中'pickedImage'是从UIImagePickerController返回的UIImage。
即使我选择了一张图片(即,pickedImage!= nil),也不会在办理登机手续时上传图片。这张图片会在Facebook上显示消息,坐标和应用信息,而不是图片。
真的希望有人可以提供帮助。
干杯, 基兰
以下是签入时调用的整个函数:
-(void)fbPostCheckInWithMessage:(NSString *)message andFriends:(NSArray *)friends {
if (![delegate.facebook isSessionValid]) {
NSLog(@"Session invalid");
[delegate.facebook authorize:[NSArray arrayWithObjects:@"publish_stream", @"offline_access", @"publish_checkins", nil]];
} else {
NSLog(@"Session valid");
}
NSMutableString *friendsIDString;
friendsIDString = [NSMutableString stringWithFormat:@"%@", [[friends objectAtIndex:0] userID]];
if ([friends count] > 1) {
for (User *f in taggedFriends) {
if (f != [taggedFriends objectAtIndex:0]) {
[friendsIDString appendString:[NSString stringWithFormat:@", %@", f.userID]];
}
}
}
NSLog(@"Tagged Friends: %@", friendsIDString);
SBJSON *jsonWriter = [SBJSON new];
NSMutableDictionary *locationDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f", userLocation.coordinate.latitude], @"latitude",
[NSString stringWithFormat:@"%f", userLocation.coordinate.longitude], @"longitude", nil];
NSString *serialisedCoordinates = [jsonWriter stringWithObject:locationDictionary];
NSData *pictureData = UIImagePNGRepresentation(pickedImage);
NSLog(@"picture: %@", pictureData);
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:[placeDictionary objectForKey:@"id"] , @"place",
serialisedCoordinates, @"coordinates",
pictureData, @"message",
pickedImage, @"picture",
@"Via the official REDACTED app", @"application", nil];
[delegate.facebook requestWithGraphPath:@"me/checkins" andParams:parameters andHttpMethod:@"POST" andDelegate:fbCheckInResultHandler];
}
我正在使用friendsIDString来获取用户所拥有的朋友的ID。我已从此处的示例中删除了此功能,因为它已全部注释掉,因为我试图找出导致问题的原因(这是照片标记)。只想清除它。
- UPDATE-- 这是我设置pickedImage的方式。我使用UIImagePickerController中的委托方法:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
NSLog(@"Picked image");
pickedImage = [[UIImage alloc] init];
pickedImage = image;
[imagePickerController dismissModalViewControllerAnimated:YES];
}
答案 0 :(得分:1)
您可以使用以下方式将照片上传到Facebook: - UIImagePNGRepresentation,UIImageJPEGRepresentation等基于图像类型或来自UIImage类的imagewithdata ..
NSData *yourImageData= UIImagePNGRepresentation(yourImage);
初始化词典: -
NSMutableDictionary *mutableDictWithParam= [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Your text", @"message", yourImageWithData, @"theSource", nil];
最后发帖子: -
[facebook requestWithGraphPath:@"/me/photos" andParams:mutableDictWithParam andHttpMethod:@"POST" andDelegate:self];
在你的应用程序中,我认为你还没有初始化你的NSData对象(pickimage)..看起来一切都很好。
根据我们的讨论您可以使用它来压缩图像: -
NSData* UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
NSData *compressedImage = UIImagePNGRepresentation(yourIMage, .5)//quality is ensured through second argument lies between o and 1