无法在ios 11.0.3中上传图像

时间:2017-10-23 05:35:49

标签: ios swift3 uiimage alamofire uiimagepickercontroller

我面临一个非常奇怪的问题。我试图选择一个图像,并在裁剪后将其上传到服务器。我无法在ios 11.0.3上运行的iphone 5上执行此操作。但在运行ios 10.3.3的iphone 5上运行正常。

我使用以下代码从图库中选择图片:

    func showImgPicker()
    {
        self.imagePicker.allowsEditing = false
        self.imagePicker.sourceType = .photoLibrary
        self.present(self.imagePicker, animated: true, completion: nil)
    }

    @IBAction func getImgs(_ sender: UIButton) {



        let photos = PHPhotoLibrary.authorizationStatus()
        if photos == .notDetermined {
            PHPhotoLibrary.requestAuthorization({status in
                if status == .authorized{
                    self.showImgPicker()
                   } else {

                }
            })
        }
        else
        {
            showImgPicker()
        }

    }

   @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let imageURL = info[UIImagePickerControllerReferenceURL] as! NSURL

        picker.dismiss(animated: true, completion: nil)
        dismiss(animated: true) {
            self.delegate?.presentEditor(img: (info[UIImagePickerControllerOriginalImage] as? UIImage)!, id: self.id!, type: self.commentType)
        }

    }

一旦图像被挑选出来,我就会发送它进行封送,发布图片保存在应用的本地文档目录中:

 static func SaveImgLocal(img: UIImage, tsStr: String) -> String
{
    let fileManager = FileManager.default
     do {
        let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
        let fileURL = documentDirectory.appendingPathComponent("\(tsStr).jpeg")

        if let imageData = UIImageJPEGRepresentation(img, 1) {
            try imageData.write(to: fileURL)
            return "\(tsStr).jpeg"
        }
        else
        {
            return Constants.MESSAGE_ERROR
        }
} catch {
    return Constants.MESSAGE_ERROR + " " + error.localizedDescription
}
}

最后我将图像上传到服务器:

let url2 = URL(string: "file:///private\((url?.absoluteString)!)")

            print(url2)
            print(url)


            let salesObj = try SalesFactory.getSalesService()

            try uploadDoc(url: Urls.uploadImgUrl, docUrl: url2!, parseResponse: { (result) in
                print(result)
           }


 public static func uploadDoc(url: String, docurl: URL, httpResponse: @escaping (DataResponse<Any>) -> Void) throws
{


    let hdr: HTTPHeaders = ["Accept": "application/json"]




    Alamofire.upload(docurl, to: url, method: .post, headers: hdr).responseJSON { (response) in
        print(response)
        httpResponse(response)
    }

}

相同的代码适用于ios 10.3.3,但不适用于ios 11.0.3。没有错误。服务器发送提示上传失败的消息。

1 个答案:

答案 0 :(得分:0)

在info.plist中添加了NSPhotoLibraryAddUsageDescription并压缩了图像。它开始工作了。

如果遇到此问题,请在info.plist中添加NSPhotoLibraryUsageDescription和NSPhotoLibraryAddUsageDescription。