OStack程序员社区-中国程序员成长平台

标题: ios - 附加图像并发布到 JSON API [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:09
标题: ios - 附加图像并发布到 JSON API

如何使用图片创建帖子并将其作为 iPhone 应用的特色图片附加?

我正在使用带有 JSON API plugin 的 wordpress

我创建帖子的网址:<-- Reference to this

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

现在我需要将手机图库中的图片附加到此网址。我该怎么做?



Best Answer-推荐答案


试试这个。使用 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"OST"];

    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];
}

希望对您有所帮助。

关于ios - 附加图像并发布到 JSON API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10662907/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4