我将从图库中挑选的图片转换为其网址,如此...
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
中的参数中,我没有得到图片的网址。上面我得到了localPath
中的网址。但我无法遍历localPath
,因为它会出错。另外,在imageArray
中,我传递了不是网址格式的图片......它采用以下格式:<UIImage: 0x60800009f9f0> size {4288, 2848} orientation 0 scale 1.000000...
如何将网址格式传递到{{} 1}},我无法理解。这可能是问题吗?
另外,我如何获取图像的网址,以便将其传递给API调用...?请帮忙......
答案 0 :(得分:0)
您在我的问题中理解的是,您正在将图像设置为imageview,并且您希望将该图像的路径传递给api。 所以我写了一个函数来做同样的调整大小,你可以根据自己的需要避免或修改。
因此,传递图像选择器拾取的图像和默认名称字符串(例如:&#34; image1&#34;),您将得到图像路径/网址作为回报 func storeImage(image:UIImage,fileName:String) - &gt;字符串{ var resizedImage = image
func storeImage(image:UIImage, fileName:String) -> String {
var resizedImage = image
if (image.size.width > 200) {
let width = 200.0 as CGFloat
let height = image.size.height * width / image.size.width
resizedImage = image.scaleImage(toSize: CGSize(width: width, height: height))!
}
else if (image.size.height > 200) {
let height = 200.0 as CGFloat
let width = image.size.width * height / image.size.height
resizedImage = image.scaleImage(toSize: CGSize(width: width, height: height))!
}
let imageData = NSData(data:UIImagePNGRepresentation(resizedImage)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs: String = paths[0]
let fullPath = docs.stringByAppendingPathComponent(path: fileName)
_ = imageData.write(toFile: fullPath, atomically: true)
return fullPath
}