OStack程序员社区-中国程序员成长平台

标题: ios - 无法在 ios 11.0.3 中上传图片 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:03
标题: ios - 无法在 ios 11.0.3 中上传图片

我正面临一个非常奇怪的问题。我正在尝试选择图像并在裁剪后将其上传到服务器。我无法在运行 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。没有错误。服务器发送一条消息,提到上传失败。



Best Answer-推荐答案


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

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

关于ios - 无法在 ios 11.0.3 中上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46882548/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4