为什么有些jpg照片不能在swift 3中上传?

时间:2017-07-18 09:42:50

标签: ios arrays swift3 image-uploading

在我的应用中有一个上传页面可以正常使用png文件但是对于一些jpg文件它没有上传图像 - 记住我想上传图像数组

这是我的代码 - 如果您需要更多代码请告诉我,我会在这里编辑我的代码但这些代码有问题

func uploadify(UploadUrl: String , photoBase64 : String) -> String {
    var request = URLRequest(url: URL(string: "http://example.com/api/"+UploadUrl+"?api_token=\(EmailSignInViewController.api_token)")!)
    print(request)
    request.httpMethod = "POST"
    let postString = "myfile=\(photoBase64)"
    print(postString)

    request.httpBody = postString.data(using: .utf8)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print("error=\(error)")
            DispatchQueue.main.async {
                let alertController = UIAlertController(title: "Error", message: "ServerError", preferredStyle: UIAlertControllerStyle.alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){
                    (result : UIAlertAction) -> Void in
                }
                alertController.addAction(okAction)
                self.present(alertController, animated: true, completion: nil)
                alertController.view.tintColor = UIColor.red
                alertController.view.backgroundColor = UIColor.red
                alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width
            }

           return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = String(data: data, encoding: .utf8)
        firstLevelViewController.progressing -= 1
        self.UpdateState()
        print("responseString = \(responseString)")
    }

    task.resume()

    return ""
}


func uploadFiles() {
    let UploadUrl = NSURL()

    ///////Start Json////////
    var request = URLRequest(url: URL(string: "http://example.com/api?api_token=\(EmailSignInViewController.api_token)")!)
    request.httpMethod = "POST"

    var sendingString = [String]()
    firstLevelViewController.progressing = translateTableViewController.imageUpload.count
    firstLevelViewController.progressingTotal = translateTableViewController.imageUpload.count
    print(translateTableViewController.imageUpload)
    for i in  0...translateTableViewController.imageUpload.count - 1 {
        print(i)
        print(translateTableViewController.imageUpload[i])            

        var siavashimg = translateTableViewController.imageUpload[i];
        print(siavashimg.size)

        let cc = siavashimg
        print(cc)
        if let siavash = UIImageJPEGRepresentation(siavashimg , 1.0){
            if let imageData : NSData = siavash as NSData! {
                let imageStr = imageData.base64EncodedString(options: .endLineWithCarriageReturn)

                self.uploadify(UploadUrl: "fileIOS", photoBase64: imageStr)
                print("JPG")
            }
            else if let imageData : NSData = UIImagePNGRepresentation(translateTableViewController.imageUpload[i]) as! NSData {
                let imageStr = imageData.base64EncodedString(options: .endLineWithCarriageReturn)
                self.uploadify(UploadUrl: "fileIOS", photoBase64: imageStr)
                print("PNG")
            }
        }

    }

0 个答案:

没有答案