作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用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/
我无法在附加行中显示“真”、“假”、"is"和“否”按钮。 我在这里有一个应用程序:Application 请按照以下步骤使用应用程序: 1。当你打开应用程序时,你会看到一个绿色的加号按钮,点击 在此
我是一名优秀的程序员,十分优秀!