本文介绍了使用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
            ""
        }
    )

我收到此错误:

任何想法?

推荐答案

// 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 2 + AFNetworking发布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 23:20