Here is the piece of code i have used to get the images from url but the code crashes with the error posted below.Please someone help me to sort this.
下面是我用来从url获取图像的代码片段,但是代码会崩溃,错误发布在下面。请谁来帮我整理一下。
let imageurl = Gift.imagesUrl.replacingOccurrences(of: "\n", with: "", options: .regularExpression)
Alamofire.request(imageurl, method: .get).responseImage { response in
print(response)
}
ERROR:
错误:
FAILURE: invalidURL("( \"https://s3.amazonaws.com/webdevapp/app/public/spree/products/2/product/ri1.jpg?1476104874\")")
3 个解决方案
#1
0
I can't let the solution being:
我不能让解决方案存在:
let string = urlstring.replacingOccurrences(of: "[\n \" )(]", with: "", options: .regularExpression, range: nil)
That's a hacky solution, not a viable one. It's a bad solution.
这是一种陈腐的解决方案,而不是可行的解决方案。这是一个糟糕的解决方案。
What is the real issue? The parsing to get the imagesUrl
is wrong.
真正的问题是什么?解析获取imagesUrl是错误的。
You did this:
你这样做:
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)
}
What's wrong?
Don't use NSArray
in Swift3+. Use Swift Array.
Don't use String(describing:)
, that's not doing what you think it does. It's calling description
method of the object (in your case an NSArray
), that's why it added (
, \n
, "
, )
. You want the object, not the description.
怎么了?不要在Swift3+中使用NSArray。使用快速数组。不要用String(description:),那不是你认为的那样。它调用对象的描述方法(在你的例子中是NSArray),这就是它添加(,\n,)的原因。你想要的是对象,而不是描述。
Don't call your var imagesUrl
with an "s" if it's only one, or you are only interested in the first one? That's unclear, and a misleading name.
如果你的var imagesUrl只有一个,不要用“s”来调用它,或者你只对第一个感兴趣?这是一个不清楚的、具有误导性的名字。
It should be:
应该是:
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.
}
or:
或者:
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
}
Then, you just do:
然后,你做的事:
Alamofire.request(Gift.imagesUrl).responseImage{ response in
print(response)
}
or (using the array)
或(使用数组)
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)
}
#2
0
Use this Code
使用这个代码
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)
}
}
}
#3
0
let urlstring = Gift.imagesUrl
let string = urlstring.replacingOccurrences(of: "[\n \" )(]", with: "", options: .regularExpression, range: nil)
Alamofire.request(string).responseImage{ response in
print(response)
}
#1
0
I can't let the solution being:
我不能让解决方案存在:
let string = urlstring.replacingOccurrences(of: "[\n \" )(]", with: "", options: .regularExpression, range: nil)
That's a hacky solution, not a viable one. It's a bad solution.
这是一种陈腐的解决方案,而不是可行的解决方案。这是一个糟糕的解决方案。
What is the real issue? The parsing to get the imagesUrl
is wrong.
真正的问题是什么?解析获取imagesUrl是错误的。
You did this:
你这样做:
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)
}
What's wrong?
Don't use NSArray
in Swift3+. Use Swift Array.
Don't use String(describing:)
, that's not doing what you think it does. It's calling description
method of the object (in your case an NSArray
), that's why it added (
, \n
, "
, )
. You want the object, not the description.
怎么了?不要在Swift3+中使用NSArray。使用快速数组。不要用String(description:),那不是你认为的那样。它调用对象的描述方法(在你的例子中是NSArray),这就是它添加(,\n,)的原因。你想要的是对象,而不是描述。
Don't call your var imagesUrl
with an "s" if it's only one, or you are only interested in the first one? That's unclear, and a misleading name.
如果你的var imagesUrl只有一个,不要用“s”来调用它,或者你只对第一个感兴趣?这是一个不清楚的、具有误导性的名字。
It should be:
应该是:
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.
}
or:
或者:
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
}
Then, you just do:
然后,你做的事:
Alamofire.request(Gift.imagesUrl).responseImage{ response in
print(response)
}
or (using the array)
或(使用数组)
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)
}
#2
0
Use this Code
使用这个代码
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)
}
}
}
#3
0
let urlstring = Gift.imagesUrl
let string = urlstring.replacingOccurrences(of: "[\n \" )(]", with: "", options: .regularExpression, range: nil)
Alamofire.request(string).responseImage{ response in
print(response)
}