菜鸟教程小白 发表于 2022-12-11 19:03:55

ios - 无法在 ios 11.0.3 中上传图片


                                            <p><p>我正面临一个非常奇怪的问题。我正在尝试选择图像并在裁剪后将其上传到服务器。我无法在运行 ios 11.0.3 的 iphone 5s 上执行此操作。但同样适用于运行 ios 10.3.3 的 iphone 5。 </p>

<p>我正在使用以下代码从图库中选择图片:</p>

<pre><code>    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: ) {

    let imageURL = info as! NSURL

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

    }
</code></pre>

<p>一旦图像被选中,我将其发送以进行复制,并发布图像正在保存在应用程序的本地文档目录中:</p>

<pre><code> static func SaveImgLocal(img: UIImage, tsStr: String) -&gt; String
{
    let fileManager = FileManager.default
   do {
      let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
      let fileURL = documentDirectory.appendingPathComponent(&#34;\(tsStr).jpeg&#34;)

      if let imageData = UIImageJPEGRepresentation(img, 1) {
            try imageData.write(to: fileURL)
            return &#34;\(tsStr).jpeg&#34;
      }
      else
      {
            return Constants.MESSAGE_ERROR
      }
} catch {
    return Constants.MESSAGE_ERROR + &#34; &#34; + error.localizedDescription
}
}
</code></pre>

<p>最后我将图像上传到服务器:</p>

<pre><code>let url2 = URL(string: &#34;file:///private\((url?.absoluteString)!)&#34;)

            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&lt;Any&gt;) -&gt; Void) throws
{


    let hdr: HTTPHeaders = [&#34;Accept&#34;: &#34;application/json&#34;]




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

}
</code></pre>

<p>相同的代码适用于 ios 10.3.3,但不适用于 ios 11.0.3。没有错误。服务器发送一条消息,提到上传失败。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 info.plist 中添加 NSPhotoLibraryAddUsageDescription 并压缩图像。它开始工作了。 </p>

<p>任何遇到此问题的人,请在 info.plist 中添加 NSPhotoLibraryUsageDescription 和 NSPhotoLibraryAddUsageDescription。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法在 ios 11.0.3 中上传图片,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46882548/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46882548/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法在 ios 11.0.3 中上传图片