发布的图像与夫特2 + AFNetworking(Posting image with Swift

2019-10-24 15:41发布

我有以下代码

let manager = AFHTTPRequestOperationManager()
    manager.POST(
        "http://shipeala.redmintlabs.com/api/orders/create",
        parameters: params,
        constructingBodyWithBlock: { (formData : AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(SHOrderImage.getInstance().image!, name: "photo", fileName: "image.jpg", mimeType: "image/*")
        },
        success: { (operation: AFHTTPRequestOperation!,
            ""
        },
        failure: { (operation: AFHTTPRequestOperation!,
            error: NSError!) -> Void in
            ""
        }
    )

而我得到这个错误:

不能援引 'POST' 有型“(字符串,参数的参数列表:NSDictionary中,constructingBodyWithBlock:(AFMultipartFormData) - >太虚,成功!(AFHTTPRequestOperation!AnyObject) - >太虚,失败!(AFHTTPRequestOperation!NSError! ) - >无效)”

有任何想法吗??

Answer 1:

// add this line of code for image to NSData conversion
let imageData = UIImageJPEGRepresentation(UIImage(named: "yourImageNAme"), 0.5)


let op : AFHTTPRequestOperation = manager.POST("", parameters: parameters, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(imageData!, name: "Photo", fileName: "photo.jpg", mimeType: "image/jpeg")
            },
            success:
            {
                (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in

                   print(responseObject)                 

            },
            failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
                print(error)

        })!


op.start()


Answer 2:

你可以不喜欢这种方式,

        var manager = AFHTTPRequestOperationManager()
        var operation = manager.POST("http://shipeala.redmintlabs.com/api/orders/create", parameters: params, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(imageData, name: "Photo", fileName: "photo.jpg", mimeType: "image/jpeg")
        }, success: { (operation, responseObject) -> Void in
            println(responseObject)
        }) { (operation, error) -> Void in
            println(error)
        }

        operation.start()


Answer 3:

你已经忘记了返回类型“ - >虚空”的成功处理程序。 这一次编译:

    manager.POST("", parameters: params, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
        //
        }, success: { (operation: AFHTTPRequestOperation!, response: AnyObject?) -> Void in
        //
        }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
        //
    }


Answer 4:

上传使用AFNetworking图像斯威夫特4

func uploadUsingPOST_Image (urlPath: NSString, withParameter dictData: NSMutableDictionary, withImage image: UIImage, WithImageName imageName: NSString,successBlock success:@escaping (_ responceData:AnyObject)->Void) {

    let urlPath1 = "Url path here"

    let manager = AFHTTPRequestOperationManager()

    var Timestamp: String {
        return "\(NSDate().timeIntervalSince1970 * 1000)"
    }



    let operation =  manager.post(urlPath1 as String, parameters: dictData, constructingBodyWith: { (data:AFMultipartFormData!) -> Void in

        let image1: UIImage? = self.resizeImage(image: image, targetSize: CGSize.init(width: 400.0, height: 400.0))

        if image1 != nil {

            data.appendPart(withFileData: UIImagePNGRepresentation(image1!)!, name: imageName as String, fileName: "\(Timestamp).png", mimeType: "image/png")


        }
    }, success: { (operation, responseObject) -> Void in

        success(responseObject as AnyObject)
    }) { (operation, error) -> Void in
        print(error, terminator: "")
    }

    operation?.start()
}


文章来源: Posting image with Swift 2 + AFNetworking