开发者社区> 问答> 正文

iOS 利用http上传图片出错

迪迪控 2017-11-04 14:31:39 1111

你好,我根据后台返回的oss_access_key_id,url,policy,signature,做post请求,但是请求出错,实在不知道问题出在哪里

下面是代码片段:

  • (void)postOSSImageKeybucketName:(NSString )oss_access_key_id url:(NSString )url policy:(NSString )policy Signature:(NSString )signature{

    if (_currentImg == nil || !_currentImg) {

    return;

    }
    //2. 图片名字
    NSString *fileName = [ShareMethod ret20bitString];
    fileName = [fileName stringByAppendingString:@".jpg"];
    //3. 图片二进制文件
    NSData *imagedata = UIImageJPEGRepresentation(_currentImg, 0.00001);
    //4. 发起网络请求
    MySessionManager *manager = [MySessionManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:fileName forKey:@"key"];
    [dic setValue:oss_access_key_id forKey:@"OSSAccessKeyId"];
    [dic setValue:policy forKey:@"policy"];
    [dic setValue:signature forKey:@"Signature"];
    [dic setValue:imagedata forKey:@"file"];
    // [dic setValue:fileName forKey:@"MyFilename.jpg"];

// [dic setValue:@"200" forKey:@"success_action_status"];

[manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    [formData appendPartWithFileData:imagedata name:fileName fileName:fileName mimeType:@"image/jpeg"];

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    NSLog(@"%@",responseObject);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
    NSLog(@"++++%ld",(long)response.statusCode);
}];

}

iOS开发
分享到
取消 提交回答
全部回答(1)
  • 一撕得杨阳
    2019-07-17 21:42:20

    // 应该是第三步 图片转成二进制文件出现问题 改为下边的写法 判断image是否是png格式的
    if (UIImagePNGRepresentation(image) == nil) {

    imageData = UIImageJPEGRepresentation(image, 1);

    } else {

    imageData = UIImagePNGRepresentation(image);

    }

    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题