gpt4 book ai didi

ios - ios 11.0.3无法上传图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:00 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com