使用 Swift 2 + AFNetworking 发布图片

Posting image with Swift 2 + AFNetworking

我有以下代码

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
            ""
        }
    )

我收到了这个错误:

Cannot invoke 'POST' with an argument list of type '(String, parameters: NSDictionary, constructingBodyWithBlock: (AFMultipartFormData!) -> Void, success: (AFHTTPRequestOperation!, AnyObject!) -> Void, failure: (AFHTTPRequestOperation!, NSError!) -> Void)'

有什么想法吗??

您忘记了成功处理程序的 return 类型“-> Void”。这个编译:

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

你可以这样做,

        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()
// 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()

在 Swift 4

中使用 AFNetworking 上传图像
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()
}