Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用alamofire图像从json数组检索图像?_Ios_Swift_Alamofire - Fatal编程技术网

Ios 如何使用alamofire图像从json数组检索图像?

Ios 如何使用alamofire图像从json数组检索图像?,ios,swift,alamofire,Ios,Swift,Alamofire,这是我用来从url获取图像的一段代码,但是代码由于下面发布的错误而崩溃。请有人帮我排序 let imageurl = Gift.imagesUrl.replacingOccurrences(of: "\n", with: "", options: .regularExpression) Alamofire.request(imageurl, method: .get).responseImage { response in print(response) } 错误: FAILURE

这是我用来从url获取图像的一段代码,但是代码由于下面发布的错误而崩溃。请有人帮我排序

let imageurl = Gift.imagesUrl.replacingOccurrences(of: "\n", with: "", options: .regularExpression)

Alamofire.request(imageurl, method: .get).responseImage { response in
    print(response)
}
错误:

 FAILURE: invalidURL("(    \"https://s3.amazonaws.com/webdevapp/app/public/spree/products/2/product/ri1.jpg?1476104874\")")
使用此代码

      let ImageURL = URL(string: "your image URL")!

      Alamofire.request(ImageURL).responseData { (response) in
         if response.error == nil {
            print(response.result)

           // Show the downloaded image:
             if let data = response.data {
                 self.downloadImage.image = UIImage(data: data)
            }                
        }
    }          

我不能让解决方案是:

let string = urlstring.replacingOccurrences(of: "[\n \" )(]", with: "", options: .regularExpression, range: nil)
这是一个老套的解决方案,不是可行的。这是一个糟糕的解决方案

真正的问题是什么?获取
imagesUrl
的解析错误

你做到了:

var _imagesUrl:String? 
var imagesUrl:String 
{ 

    if _imagesUrl == nil 
    { 
        _imagesUrl = "" 
    } 
    return _imagesUrl! 
} 

if let image = giftherData["master_variant_images"] as? NSArray 
{
    self._imagesUrl = String(describing: image) 
}
怎么了?
不要在Swift3+中使用
NSArray
。使用Swift数组
不要使用
字符串(description:)
,这并不是你所认为的那样。它正在调用对象的
描述
方法(在您的例子中是
NSArray
),这就是为什么它添加了
\n
。您想要的是对象,而不是描述

如果您的var
imagesUrl
只有一个,或者您只对第一个感兴趣,请不要用“s”来称呼它?这不清楚,而且是一个误导性的名称

应该是:

if let imageStringURLs = giftherData["master_variant_images"] as? [String] { //Yes, use a Swift Array of String, not a NSArray
    self._imagesUrl = imageStringURLs.first //I took presumption that you are only interested in the first one, of not, then use an array to save them, not a String.
}  
或:

然后,你只要做:

Alamofire.request(Gift.imagesUrl).responseImage{ response in
        print(response)
}
或(使用数组)


Gift.imagesUrl.replacingOccurrences(of:…)
为什么?什么是Gift.imagesUrl
真的?你是如何得到它的?你需要正确地解析和使用它。你能发布你是如何为Gift.imagesUrl设置值的吗?通常我得到的响应是:FAILURE:invalidURL((\n\“\”\n)”)…/n“作为附加组件包含在json响应中,而json响应中不存在该附加组件。您能告诉我如何在没有“/n”.var\u imagesUrl:String的情况下从服务器检索正确的图像url吗?var imagesUrl:String{if{u imagesUrl==nil{{u imagesUrl=”“}返回{u imagesUrl!}如果let image=giftherData[“master\u variant\u images”]as?NSArray{self.\u imagesUrl=String(描述:image)}。。。Gift是模型对象类名称请自行回答您的问题,而不是使用您的解决方案编辑问题我得到了失败:invalidURL((\n\“\”\n)”)我得到了解决方案…….let urlstring=Gift.imagesUrl let string=urlstring.replacingOccurrences(of:“[\n\”(]”),with:,选项:。regularExpression,range:nil)这是一个黑客解决方案。真正的问题是错误的解析。在最终交付中,这不是一个可行的解决方案。
var _imagesUrl: [String]?
var imagesUrl: [String]?
//if nil as you did
if let imageStringURLs = giftherData["master_variant_images"] as? [String] { //Yes, use a Swift Array of String, not a NSArray
    self._imagesUrl = imageStringURLs
}  
Alamofire.request(Gift.imagesUrl).responseImage{ response in
        print(response)
}
let firstURL = Gift.imagesUrl[0] //Or if it's the second let secondURL = Gift.imagesUrl[1], etc.
Alamofire.request(firstURL).responseImage{ response in
        print(response)
}