Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphone图形api无法在facebook中发布图像_Iphone - Fatal编程技术网

iphone图形api无法在facebook中发布图像

iphone图形api无法在facebook中发布图像,iphone,Iphone,使用此方法发布图像 - (void)postInAppFacebook { btnForLogout.hidden=NO; NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3]; NSData *imageData = UIImageJPEGRepresentation(imgPicture.image, 4.0); UIImage *picture

使用此方法发布图像

- (void)postInAppFacebook {

    btnForLogout.hidden=NO;
    NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3];

    NSData *imageData = UIImageJPEGRepresentation(imgPicture.image, 4.0);


    UIImage *picture = [[UIImage alloc]initWithData:imageData];


    FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];


    [variables setObject:graph_file forKey:@"file"];

    [variables setObject:[NSString stringWithFormat:@"%@", txtComment.text] forKey:@"message"];




    **[fbGraph doGraphPost:@"me/photos" withPostVars:variables];**




    NSLog(@"Now log into Facebook and look at your profile & photo albums...");
    UIAlertView *alertForImageHasPost=[[UIAlertView alloc]initWithTitle:@"MAP IMAGE POST" message:@"SELECTED IMAGE POSTED" delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:@"OK",nil];
    [alertForImageHasPost show];
    [alertForImageHasPost release];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];  
}
此方法中的问题 第二次崩溃,但在模拟器中运行良好

- (FbGraphResponse *)doGraphPost:(NSString *)action withPostVars:(NSDictionary *)post_vars {
    NSLog(@"action is %@",action);
    FbGraphResponse *return_value = [[FbGraphResponse alloc] init];

    NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@", action];

    NSURL *url = [NSURL URLWithString:urlString];
    NSString *boundary = @"----1010101010";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSEnumerator *enumerator = [post_vars keyEnumerator];
    NSString *key;
    NSString *value;
    NSString *content_disposition;

    //loop through all our parameters 
    while ((key = (NSString *)[enumerator nextObject])) {

        //if it's a picture (file)...we have to append the binary data
        if ([key isEqualToString:@"file"]) {

            /*
             * the FbGraphFile object is smart enough to append it's data to 
             * the request automagically, regardless of the type of file being
             * attached
             */
            FbGraphFile *upload_file = (FbGraphFile *)[post_vars objectForKey:key];
            [upload_file appendDataToBody:body];

        //key/value nsstring/nsstring
        } else {
            value = (NSString *)[post_vars objectForKey:key];

            content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key];
            [body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];

        }//end else

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    }//end while

    //add our access token
    [body appendData:[@"Content-Disposition: form-data; name=\"access_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[accessToken dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //button up the request body
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];
    [request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];

    //quite a few lines of code to simply do the business of the HTTP connection....
    NSURLResponse *response;
    NSData *data_reply;
    NSError *err;
**//crash after this  line**
    ***

> data_reply = [NSURLConnection sendSynchronousRequest:request
> returningResponse:&response error:&err];

***
    return_value.htmlResponse = (NSString *)[[NSString alloc] initWithData:data_reply encoding:NSUTF8StringEncoding];

    if (err != nil) {
        return_value.error = err;
    }

    /*
     * return the json array.  we could parse it, but that would incur overhead 
     * some users might not want (not to mention dependencies), besides someone 
     * may want raw strings back, keep it simple.
     *
     * See:  http://code.google.com/p/json-framework for an easy json parser
     */

    return return_value;
}

看看下面的函数。我希望你能成功

请查看以下所有方法:

- (IBAction)uploadPhoto:(id)sender {
    [self postOnFacebook];
}
//在Facebook功能上发布消息和照片

-(void)postOnFacebook
{
NSString *strPostMessage = [self createMessageForPostOnFacebook:74.112 withLongitude:21.85];
//UIImage *img  = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:objClsProductSearch.strProductImageURL]]];
//UIImage *img  = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"]]];
UIImage *img  = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://d1xzuxjlafny7l.cloudfront.net/wp-content/uploads/2011/08/HUDTutorial.jpg"]]];

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               strPostMessage,@"message",img, @"source",
                               nil];

[_facebook requestWithGraphPath:@"/me/photos"
                      andParams:params
                  andHttpMethod:@"POST"
                    andDelegate:self];

[img release];
}
//创建用于在Facebook上发布的消息

-(NSString *)createMessageForPostOnFacebook:(double)pfltLatitude withLongitude:(double)pfltLongitude
{
//NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pfltLatitude, pfltLongitude];
//NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",@"", @""];
//NSError* error;
//NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
//NSArray *listItems = [locationString componentsSeparatedByString:@","];
NSString *strMessage = nil;
/*if([[listItems objectAtIndex:0] isEqualToString:@"200"]) 
 {
 strMessage = [NSString stringWithFormat:@"I found a %@ using BH for iPhone \n Location : %@ \n Latitude : %f &  Longitude : %f \n Product Rating : %d",objClsProductSearch.strCategoryname,[locationString substringFromIndex:6],appDelegate.dblLatitude,appDelegate.dblLongitude,btnTmp.tag];
 }*/
strMessage = [NSString stringWithFormat:@"I found a %@ using BH for iPhone \n Location : %@ \n Latitude : %f &  Longitude : %f \n Product Rating : %d", @"Cat Name", @"Location", 74.112, 21.85, 1];

return strMessage;
}
我希望您能成功地使用上述代码

如果有任何困难,请告诉我

在你的应用程序委托中,看看下面的方法,不管它是否被实现

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    //return [[controller facebook] handleOpenURL:url];
    //return [[objPage2ViewController facebook] handleOpenURL:url];
    return [[[self.navigationController topViewController] facebook] handleOpenURL:url];
}
干杯