简体   繁体   中英

about Almofire Upload

I want to upload pictures to the server using Alamofire.

I have changed the selected photo from imagePicker to jpegData and used it in append of MultipartFormData.

When sending a communication, the memory of the app increases rapidly and when using debugPrint, a huge amount of code is extracted.

What is the problem?

I put my code below.

let header: HTTPHeaders = [
        "Content-Type":"multipart/form-data; boundary=\(boundary)",
        "access_token":"\(UserDefaults.standard.string(forKey: "token")!)"
    ]

    AF.upload(multipartFormData: { (MultipartFormData) in

        MultipartFormData.append(self.imageData, withName: "img", fileName: "img", mimeType: "img/jpeg")
        MultipartFormData.append((self.titleTextField.text?.data(using: .utf8))!, withName: "content")
    }, to: "https://httpbin.org/post", method: .post, headers: header).responseJSON { (result) in
            debugPrint(result)

    }


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let pickedImage = info[.originalImage] as? UIImage else { return }
    imageView.image = pickedImage
    image = pickedImage
    imageData = pickedImage.jpegData(compressionQuality: 0)
    imageViewNilLbl.isHidden = true
    picker.dismiss(animated: true, completion: nil)
}

enter image description here

I used this method and everything is ok in my case. Try like this.

public func upload(method: HTTPMethod = .post, endpoint: String, params: NSDictionary? = nil, image: UIImage) {

    let url = BaseUrl.url + endpoint

    AF.upload(multipartFormData: { multipartFormData in
        if let params = params {
            for (key, value) in params {
                guard let data = "\(value)".data(using: String.Encoding.utf8) else { continue }
                multipartFormData.append(data, withName: key as? String ?? "")
            }
        }

        // Append image
        attachDataToMultipart(file: image.jpegData(compressionQuality: 0.5), withName: "file", fileType: .photo, multipartFormData: multipartFormData, mimeType: .photo)
    }, to: url, method: method, headers: nil).responseJSON { (result) in
        debugPrint(result)
    }
}

func attachDataToMultipart(file: Data?, withName: String, fileType: UploadRequestFileTypeEnum, multipartFormData: MultipartFormData, mimeType: UploadMimeTypeEnum) {
    guard let data = file else { return }
    let name = UUID().uuidString + fileType.rawValue
    multipartFormData.append(data, withName: withName, fileName: name, mimeType: mimeType.rawValue)
}

enum UploadRequestFileTypeEnum: String {
    case photo = ".jpg"
    case video = ".mov"
    case file = "pdf"
}

enum UploadMimeTypeEnum: String {
    case photo = "image/jpg"
    case video = "video/quicktime"
    case file = "application/pdf"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM