gpt4 book ai didi

ios - 通过Parse框架AFNetworking2.0使用RESTAPI上传图片报错

转载 作者:行者123 更新时间:2023-11-29 12:52:54 25 4
gpt4 key购买 nike

我使用AFNetworking2.0上传图片,代码如下:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    [manager.requestSerializer setValue:PARSE_APPLICATION_ID forHTTPHeaderField:@"X-Parse-Application-Id"];    [manager.requestSerializer setValue:PARSE_REST_API_KEY forHTTPHeaderField:@"X-Parse-REST-API-Key"];    [manager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];    UIImage *image = [UIImage imageNamed:@"resource.bundle/1.jpg"];    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);    [manager POST:@"https://api.parse.com/1/files/kingiol.jpg" parameters:nil constructingBodyWithBlock:^(id formData) {        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];    } success:^(NSURLSessionDataTask *task, id responseObject) {        NSLog(@"success:%@", responseObject);    } failure:^(NSURLSessionDataTask *task, NSError *error) {        NSLog(@"Error:%@", error);    }];

运行这个并记录成功 block ,我得到上传响应:

    success:{    name = "84cff2e0-605c-44de-909f-655379884b67-kingiol.jpg";    url = "http://files.parse.com/5a56e6bf-a4b5-4155-9bf3-238550da126b/84cff2e0-605c-44de-909f-655379884b67-kingiol.jpg";    }

但是,当我复制 url,然后用 chrome 打开时,图像是无效图像。

请帮忙

最佳答案

您的文件似乎包含您提出的全部请求。尝试使用像 TextEdit 这样的文本编辑器打开该文件,我已经做到了,我发现了类似这样的内容:

--Boundary+0xAbCdEfGbOuNdArY
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
Content-Type: image/jpeg

<All binary image data>
....
..
..

--Boundary+0xAbCdEfGbOuNdArY--

为什么不使用 Parse SDK 函数?看这个:

NSData *imageData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];

PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"];
userPhoto[@"imageName"] = @"My trip to Hawaii!";
userPhoto[@"imageFile"] = imageFile;
[userPhoto saveInBackground];

参见 https://parse.com/docs/ios_guide#files-images/iOS

希望对你有帮助

关于ios - 通过Parse框架AFNetworking2.0使用RESTAPI上传图片报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22003398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com