我正在尝试让用户从相册中选择一张图片上传到API并获取一些数据。问题是我不知道从iPhone上传图像是否正确。
我一直在尝试将图片的网址上传到API。我一直试图从URL中获取字符串,但是当我尝试检索它时,它会给我一个无法确定UTF的错误。然后我使用utf16,但它给了我一个字符串,里面有一堆长达数百个字符的怪异文本字符。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/jquery.qtip.min.css">
<style>
#myTable { border-collapse: collapse; width: 100%; }
#myTable th, #myTable td { text-align: left; padding: 12px; }
#myTable tr { border-bottom: 1px solid #ddd; }
#myTable tr.header, #myTable tr:hover { background-color: #f1f1f1; }
.tdStyle{ background: #F38630; }
</style>
</head>
<body>
<table id="myTable">
<tr class="header" >
<th">Name</th>
<th">Country</th>
</tr>
<tr>
<td class="one">Alfreds Futtekiste</td>
<td class="two">Germany</td>
<td class="three">Berlin</td>
</tr>
<tr>
<td class="one">Berglunds snabbkop</td>
<td class="two">Sweden</td>
<td class="three">Berlin</td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.qtip.min.js"></script>
<script>
$(document).ready(function () {
$('.one').each(function()
{
$(this).qtip({
content: $(this).clone(),
style: { classes: 'tdStyle' },
position: { my: 'top center',
at: 'bottom center' }
});
});
$('.two').each(function()
{
$(this).qtip({
content: $('td:eq(0)').clone(),
style: { classes: 'tdStyle' },
position: { my: 'top center',
at: 'bottom center' }
});
});
});
</script>
</body>
</html>
奇怪的是,当我根本不使用UTF参数时,Xcode终端告诉我作为字符串的确切路径。它给了我以下错误:
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
let pickedImageURL = info[UIImagePickerControllerImageURL] as! URL
let pickedImageStr = try! String(contentsOf: pickedImageURL, encoding: String.Encoding.utf16)
ImageAPI.call(with: pickedImageStr)
dismiss(animated: true)
}
那么为什么当我尝试将URL作为字符串获取时,它不起作用并且给我一个字符串数百个字符长并带有一堆奇怪的符号?
如果我只是复制并粘贴终端中给出的文件位置,我会收到一条错误消息,指出该文件不存在。
答案 0 :(得分:1)
使用此
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
let pickedImageURL = info[UIImagePickerControllerImageURL] as! URL
let pickedImageStr = pickedImageURL.description
ImageAPI.call(with: pickedImageStr)
dismiss(animated: true)
}
String(contentsOf:URL)方法是按字符串的URL地址显示文件,而不是按网址创建字符串