0

因此,我正在创建一个 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-- 这是我设置pickImage的方式。我使用 UIImagePickerController 中的委托方法:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    NSLog(@"Picked image");
    pickedImage = [[UIImage alloc] init];
    pickedImage = image;
   [imagePickerController dismissModalViewControllerAnimated:YES];
}
4

1 回答 1

1

您可以使用 UIImagePNGRepresentation、UIImageJPEGRepresentation 等根据图像类型或 UIImage 类中的 imagewithdata 将照片上传到 Facebook。

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 对象(pickedimage)..否则一切似乎都很好。

根据我们的讨论,您可以使用它来压缩图像:-

NSData* UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
NSData *compressedImage = UIImagePNGRepresentation(yourIMage, .5)//quality is ensured through second argument lies between o and 1
于 2012-05-15T20:42:51.827 回答