【问题标题】:Unable to upload images in ios 11.0.3无法在 ios 11.0.3 中上传图片
【发布时间】:2018-04-03 14:23:23
【问题描述】:

我正面临一个非常奇怪的问题。我正在尝试选择图像并在裁剪后将其上传到服务器。我无法在运行 ios 11.0.3 的 iphone 5s 上执行此操作。但同样适用于运行 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。没有错误。服务器发送一条消息,提到上传失败。

【问题讨论】:

  • 有没有打印错误信息?
  • 不,没有打印错误。

标签: ios swift3 uiimage alamofire uiimagepickercontroller


【解决方案1】:

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

任何遇到这个问题的人,请在 info.plist 中添加 NSPhotoLibraryUsageDescription 和 NSPhotoLibraryAddUsageDescription。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2012-07-03
    • 2020-10-03
    • 2018-04-17
    • 2018-09-15
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多