我将我从画廊到它的URL,像这样采摘的图片问题与获取图像的URL在画廊
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL
let imageName = imageURL?.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
self.localPath = photoURL.appendingPathComponent(imageName!)
do {
try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
print("File Saved")
imageArray.append(image)
} catch { //Error }
self.collectionView.reloadData()
} else { //Error here }
self.dismiss(animated: true, completion: nil)
}
现在,如果我有2张图片,我想通过他们将我的API调用一个接一个地作为参数。这是我正在做这样的...
for imgURL in imageArray {
let url = "http://myapp.com/vw/images_upload"
let headers = [ "Content-Type":"application/x-www-form-urlencoded"]
let Parameters =
[
"image": imgURL,
"seller_id": id
] as [String : Any]
Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers)
.responseJSON { (response) in
if let httpResponse = response.response {
print("error \(httpResponse.statusCode)")
if httpResponse.statusCode == 200 {
if let result = response.result.value as? [String:Any] {
if result["success"] as! Int == 0 {
print("Something went wrong!")
} else if result["success"] as! Int == 1 {
print("UPLOADED IMAGE SUCCESSFULLY!!")
}}}}}}
但在参数时,在imgURL
,我没有得到的图像的URL。上面我得到了localPath
中的网址。但是我不能通过localPath
循环,因为它提供了一个错误。此外,在imageArray
中,我传递的图像不是url格式...它的格式如下:<UIImage: 0x60800009f9f0> size {4288, 2848} orientation 0 scale 1.000000...
url格式如何传入imageArray
,我无法理解。可以这是问题..?
另外我怎样才能得到图像的网址,以便我可以将它传递给API调用...?请帮助...
'让图像信息= [UIImagePickerControllerOriginalImage]为? UIImage'给你一个'UIImage'。然后这个图像通过'imageArray.append(image)'存储在'imageArray'中。如果要将图像的URL存储在数组中,则应该使用'imageArray.append(imageURL)'代替。 –
imageArray是一个UIImage数组,因此它会抛出错误“无法将类型'NSURL'的值转换为期望的参数类型'UIImage' – bws
如果您的'imageArray'是'[UIImage]'的类型,那么您如何期待每个元素作为URL?您必须为图像分别为您的网址创建一个单独的数组。并且您不必首先将图像上传到远程服务器,然后从那里获取URL - >将该URL保存到不同的请求。 – Aks