成员'上传的模糊引用(_:to:method:headers:interceptor :)'

问题描述 投票:1回答:1

我使用Alamofire按下按钮上传图像,但它显示错误

AF.upload(multipartFormData: { MultipartFormData方法行

(注意:Alamofire.upload在Alamofire 5.0更新后更改为AF.upload)

@IBAction func btnUploadImage(_ sender: UIButton) {

    let uploadDict = ["user_id":getUserId] as [String:String]

    AF.upload(multipartFormData: { MultipartFormData in

                let image :Data = UIImageJPEGRepresentation(self.uploadImg.image!, 1.0)!

                MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
                for(key,value) in uploadDict{

                    MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}

            }, to: "http://XXXXXXXXXXXXXXXX/uploadImage", encodingCompletion: {
                EncodingResult in
                switch EncodingResult{
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        debugPrint("SUCCESS RESPONSE: \(response)")
                    }
                case .failure(let encodingError):

                    print("ERROR RESPONSE: \(encodingError)")

                }        })

//查看此代码的控制器

导入UIKit导入Alamofire

class updateVC:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

//MARK:- OUTLETS
@IBOutlet weak var tFeditName: UITextField!
@IBOutlet weak var tFusername: UITextField!
@IBOutlet weak var tFemail: UITextField!
@IBOutlet weak var uploadImg: UIImageView!
@IBOutlet weak var tVeditAddress: UITextView!


//MARK:- DECLARATIONS

var imagePicker = UIImagePickerController()

var editName = String()
var editUserName = String()
var editEmail = String()
var editImage = UIImage()
var editAddress = String()
var getUserId = String()

//MARK:- LIFE CYCLE
override func viewDidLoad() {
    super.viewDidLoad()

    imagePicker.delegate = self

    tFeditName.text = editName
    tFusername.text = editUserName
    tFemail.text = editEmail
    uploadImg.image = editImage
    tVeditAddress.text = editAddress

}
//MARK:- CUSTOM FUNCTIONS

func postJSON() {

    let param = ["fullname":tFeditName.text!,"username":tFusername.text!,"email":tFemail.text!,"address":tVeditAddress.text!,"user_id":getUserId] as NSDictionary


    AF.request("http://XXXXXXXXXXXXXXXXXXXX/updatedetails", method: .post, parameters: param as? Parameters, encoding: URLEncoding.default).responseJSON { response in

        switch response.result{

        case .success(let json):
            print(json)

            DispatchQueue.main.async {
                print(param)
            }
        case.failure(let Error):

            print(Error)
        }
    }

}

//ImageViewPicker

func openCamera()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

func openGallery()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.allowsEditing = true
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have permission to access gallery.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {

        uploadImg.image = pickedImage

    }
    picker.dismiss(animated: true, completion: nil)
}



//MARK:- ACTIONS

@IBAction func btnSelectImage(_ sender: UIButton) {

    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallery()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true, completion: nil)


}

@IBAction func btnUploadImage(_ sender: UIButton) {

    let image: UIImage? = self.uploadImg.image

    let uploadDict = ["user_id":getUserId] as [String:String]

    AF.upload(multipartFormData: { MultipartFormData in

        let image :Data = (image?.jpegData(compressionQuality: 1))!

        MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
        for(key,value) in uploadDict{

            MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}

    }, to: "http://XXXXXXXXXXXXXXXX/uploadImage", encodingCompletion: {
        EncodingResult in
        switch EncodingResult{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint("SUCCESS RESPONSE: \(response)")
            }
        case .failure(let encodingError):

            print("ERROR RESPONSE: \(encodingError)")

        }        })

}
@IBAction func btnUpdateDetails(_ sender: UIButton) {

    postJSON()
    self.navigationController?.popViewController(animated: true)
}

}

ios swift iphone alamofire
1个回答
0
投票

我刚安装了pods

pod 'Alamofire', '~> 5.0.0-beta.5'

//示例代码

let image: UIImage? = self.uploadImg.image;

    let uploadDict = ["user_id":getUserId] as [String:String]

    Alamofire.upload(multipartFormData: { MultipartFormData in

        let image :Data = (image?.jpegData(compressionQuality: 1))!

        MultipartFormData.append(image, withName: "image" , fileName: "image.jpeg" , mimeType: "image/jpeg")
        for(key,value) in uploadDict{

            MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}

    }, to: "http://XXXXXXXXXXXXXXXX/uploadImage", encodingCompletion: {
        EncodingResult in
        switch EncodingResult{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint("SUCCESS RESPONSE: \(response)")
            }
        case .failure(let encodingError):

            print("ERROR RESPONSE: \(encodingError)")

        }        })
© www.soinside.com 2019 - 2024. All rights reserved.