2012-05-19 126 views
2

如何使用圖片創建帖子並將其作爲來自iPhone應用的精選圖片進行附加?附加圖片併發布到JSON API

我使用WordPress的與JSON API plugin

我的網址創建後:< - Reference to this

http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals 

現在我需要從手機圖庫此URL連接的圖像。我怎樣才能做到這一點?

回答

3

試試這個。使用JSON POST方法。你可以像這樣附加所有參數。

-(void)uploadImage 
{ 

    NSString *method = @""; 
    NSString *username = @""; 
    NSString *token = @""; 

    NSString *urlString = [NSString stringWithFormat:@"%@",@"yoururl"]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    NSMutableData *body = [NSMutableData data]; 

    // file 
    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 0.8); 
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // parameter username 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[username dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // parameter token 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // // parameter method 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

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


    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 

    // now lets make the connection to the web 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",returnString); 
    [returnString release]; 
} 

希望這可以幫助你。

1

要充分填滿你的要求,你需要發送圖像文件的Web服務使用POST方法

首先選擇圖像從圖片庫或把它從相機,使數據該圖片使用以下代碼。

NSString *imgName = [NSString stringWithFormat:@"ImageTest"]; 
    NSData *imgData=UIImagePNGRepresentation(imgView.image); 

    NSString* urlstring = [NSString stringWithFormat:@"http://www.example.com/api/create_post/?nonce=eea4bb4ce5&status=publish&title=Test&content=test%20content&categories=animals&imageFile=%@",imgName]; 
    NSLog(@"URL >> %@",urlstring); 

    NSURL url = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    //===============================================   
    NSMutableURLRequest postRequest = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [postRequest setURL:url]; 
    [postRequest setHTTPMethod:@"POST"]; 

    NSString boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    NSMutableData *body = [[NSMutableData alloc] init]; 
    [postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

imgName這是該代碼非常重要對象它您需要在其中如上圖所示Web服務來傳遞圖像。

 [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.png\"\r\n",imgName]] dataUsingEncoding:NSUTF8StringEncoding]]; //img name 
     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

     // Add Image imgData is Declare as Above. 
     [body appendData:[NSData dataWithData:imgData]]; 



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

    NSURLResponse *response; 
    NSError error = nil; 


    NSData* data = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error]; 
    NSString *result=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    // NSString * jsonRes = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 


    NSLog(@"result = %@",[result JSONValue]); 

它完美地與我合作!我希望你能從這裏得到幫助!