gpt4 book ai didi

ios - 获取图库中图像的 url 时出现问题

转载 作者:行者123 更新时间:2023-11-30 12:06:31 24 4
gpt4 key购买 nike

我正在将从图库中挑选的图像转换为其 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中,我没有得到图像的网址。上面我得到了localPath中的url 。但我无法循环 localPath因为它给出了一个错误。另外,在 imageArray ,我传递的图像不是 url 格式...它的格式如下:<UIImage: 0x60800009f9f0> size {4288, 2848} orientation 0 scale 1.000000...如何将url格式传入imageArray ,我无法理解。这可能是问题所在..?

另外,如何获取图像的 url,以便将其传递到 API 调用中...?请帮忙...

最佳答案

嗨,我从你的问题中了解到,你正在将图像设置为 imageview,并且你想将该图像的路径传递给 api。因此,我编写了一个函数来执行相同的操作,并具有调整大小的功能,您可以根据需要避免或修改该功能。

因此,传递图像选择器选取的图像和默认名称字符串(例如:“image1”),您将获得图像路径/url 作为返回 func storeImage(image:UIImage, fileName:String) -> String { var resizedImage = 图像

     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
}

关于ios - 获取图库中图像的 url 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46619033/

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