【问题标题】:post image on Facebook using FBConnect使用 FBConnect 在 Facebook 上发布图片
【发布时间】:2012-12-10 12:42:56
【问题描述】:

我想使用 fbconnect 在 facebook 上发布图片。

我已经编写了以下代码行,但是我应该对我的代码进行哪些更改才能在 facebook 上发布图片。

- (void) postOnPage
{
    [self saveAccessTokenKeyInfo];

    NSString *imgStr=@"hello";

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Hat Application", @"name",
                                   @"http://www.idhats.com/", @"link",
                                   @"HatApp", @"caption",
                                   imgStr, @"picture",
                                   nil];

    [facebook dialog:@"feed" andParams:params andDelegate:self];

}

谢谢...

【问题讨论】:

    标签: iphone facebook ios5


    【解决方案1】:
    - (void)uploadPhoto{
    NSMutableDictionary *args = [[NSMutableDictionary alloc] init];
    UIImage *img = [UIImage imageNamed:@"picture1.jpg"];
    NSData *data = UIImageJPEGRepresentation(img, 10);
    //NSData *data = UIImagePNGRepresentation(img);
    NSString *imgstr = [data base64Encoding];
    //NSString *imgstr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    //[args setObject:[UIImage imageNamed:@"antartica1.png"] forKey:@"image"];   
    [args setObject:imgstr forKey:@"image"];    // 'images' is an array of 'UIImage' objects
    uploadimgrequest = [FBRequest requestWithDelegate:self];
    [uploadimgrequest call:@"photos.upload" params:args dataParam:data];
    //[uploadimgrequest call:@"photos.upload" params:args];
    }
    
    -(NSString *)Base64Encode:(NSData *)data{
    //Point to start of the data and set buffer sizes
    int inLength = [data length];
    int outLength = ((((inLength * 4)/3)/4)*4) + (((inLength * 4)/3)%4 ? 4 : 0);
    const char *inputBuffer = [data bytes];
    char *outputBuffer = malloc(outLength);
    outputBuffer[outLength] = 0;
    
    //64 digit code
    static char Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    //start the count
    int cycle = 0;
    int inpos = 0;
    int outpos = 0;
    char temp;
    
    //Pad the last to bytes, the outbuffer must always be a multiple of 4
    outputBuffer[outLength-1] = '=';
    outputBuffer[outLength-2] = '=';
    
    /* http://en.wikipedia.org/wiki/Base64
     Text content   M           a           n
     ASCII          77          97          110
     8 Bit pattern  01001101    01100001    01101110
    
     6 Bit pattern  010011  010110  000101  101110
     Index          19      22      5       46
     Base64-encoded T       W       F       u
     */
    
    
    while (inpos < inLength){
        switch (cycle) {
            case 0:
                outputBuffer[outpos++] = Encode[(inputBuffer[inpos]&0xFC)>>2];
                cycle = 1;
                break;
            case 1:
                temp = (inputBuffer[inpos++]&0x03)<<4;
                outputBuffer[outpos] = Encode[temp];
                cycle = 2;
                break;
            case 2:
                outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xF0)>> 4];
                temp = (inputBuffer[inpos++]&0x0F)<<2;
                outputBuffer[outpos] = Encode[temp];
                cycle = 3;                  
                break;
            case 3:
                outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xC0)>>6];
                cycle = 4;
                break;
            case 4:
                outputBuffer[outpos++] = Encode[inputBuffer[inpos++]&0x3f];
                cycle = 0;
                break;                          
            default:
                cycle = 0;
                break;
        }
    }
    NSString *pictemp = [NSString stringWithUTF8String:outputBuffer];
    free(outputBuffer); 
    return pictemp;
    }
    

    【讨论】:

    • 它在 NSString *imgstr = [data base64Encoding] 上给出错误;关于 base64Encoding 方法的行未找到并生成警告 uploadimgrequest = [FBRequest requestWithDelegate:self];未找到 requestWithDelegate
    • 现在我已经更新了答案,包括 Base64Encoding 方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多