如何在 Swift 5 中将 URL 图像转换为图像并追加到数组中
How to Convert URL Images to UIImages in Swift5 and append into a array
我在 Swift 中有一个 API 调用 GET 5 用于获取图像的代码,我正在获取 url 个图像,我必须更改 url进入 UIImage 以将 urls 附加到 arrayimages=UIImage,url 的数据在那里,但它没有附加到 arrayImages。我的任务是把所有的数据图片放到collection view中,如果有别的方法再指导我,谢谢。
--->。让 arrayImages = UIImages
guard let data = response?.data(using: .utf8) else {
return
}
do {
let jsonObj = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
if jsonObj["error"] as! Bool == false {
print("galleryResponse/\(jsonObj)")
let jsonResponse = jsonObj["response"] as! [[String: Any]]
for i in 0...jsonResponse.count-1 {
let strGalleryImage = jsonResponse[i]["Gallery_Full"] as? String
if let imgurl = strGalleryImage {
self.userImageString1 = "\(USER_IMAGE_BASE_URL)\(imgurl)"
}
var imageString1: String?
var url1: URL!
imageString1 = self.userImageString1
if let imageUrlString1 = imageString1 {
url1 = URL(string: imageUrlString1)
DispatchQueue.global().async { [weak self] in
if let data = try? Data(contentsOf: url1!){
if let imagedata = UIImage(data: data){
print("YES_IMG")
if data != nil {
DispatchQueue.main.async {
print("append_IMG")
self!.arrimages.append(imagedata)
}
}
}
}
}
}
//}
}
}
} catch {
print("Unable_to_load_data:/\(error)")
}
})
}
可以使用AlamofireImage pod将图片URL转换为图片
首先,您需要安装pod文件pod 'AlamofireImage'。然后 在你的 ViewController 中导入 AlamofireImage。
这是实现它的方法。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCollectionViewCellIdentifier", for: indexPath) as! YourCollectionViewCell
Alamofire.request("your image URL in String formate").responseImage { response in
debugPrint(response)
debugPrint(response.result)
if let image = response.result.value {
cell.YourImage.image = image
}
}
return cell
}
Hi I found out the Best solution i.e, through SDWebImages.
* Fist I get the response into url and then append it to a array = (String)[]
* then I called the sdwebimages in cellforitem function...
####CODE
// getting the url images into an array of string
let jsonResponse = jsonObj["response"] as! [[String: Any]]
print("galleryResponse/\(jsonObj)")
for i in 0...jsonResponse.count-1 {
let strGalleryImage = jsonResponse[i]["Gallery_Full"] as? String
print("o12\(strGalleryImage!)")
let str = String((strGalleryImage?.dropFirst(11))!) as? String
print("LAAL\(str)")
if let imgurl = str {
self.arrimages.append(imgurl)
print("kl\(self.arrimages)")
self.collectionView.reloadData()
}
THEN ->
//Calling the images into cellforitem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
// called the url array into sd_setimages
cell.imgProfile.sd_setImage(with: URL(string: arrimages[indexPath.row]), placeholderImage: UIImage(named: "placeholder.jpg"))
return cell
}
Thanks for the answers but this is simplest solution of all...:)
我在 Swift 中有一个 API 调用 GET 5 用于获取图像的代码,我正在获取 url 个图像,我必须更改 url进入 UIImage 以将 urls 附加到 arrayimages=UIImage,url 的数据在那里,但它没有附加到 arrayImages。我的任务是把所有的数据图片放到collection view中,如果有别的方法再指导我,谢谢。
--->。让 arrayImages = UIImages
guard let data = response?.data(using: .utf8) else {
return
}
do {
let jsonObj = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
if jsonObj["error"] as! Bool == false {
print("galleryResponse/\(jsonObj)")
let jsonResponse = jsonObj["response"] as! [[String: Any]]
for i in 0...jsonResponse.count-1 {
let strGalleryImage = jsonResponse[i]["Gallery_Full"] as? String
if let imgurl = strGalleryImage {
self.userImageString1 = "\(USER_IMAGE_BASE_URL)\(imgurl)"
}
var imageString1: String?
var url1: URL!
imageString1 = self.userImageString1
if let imageUrlString1 = imageString1 {
url1 = URL(string: imageUrlString1)
DispatchQueue.global().async { [weak self] in
if let data = try? Data(contentsOf: url1!){
if let imagedata = UIImage(data: data){
print("YES_IMG")
if data != nil {
DispatchQueue.main.async {
print("append_IMG")
self!.arrimages.append(imagedata)
}
}
}
}
}
}
//}
}
}
} catch {
print("Unable_to_load_data:/\(error)")
}
})
}
可以使用AlamofireImage pod将图片URL转换为图片
首先,您需要安装pod文件pod 'AlamofireImage'。然后 在你的 ViewController 中导入 AlamofireImage。
这是实现它的方法。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCollectionViewCellIdentifier", for: indexPath) as! YourCollectionViewCell
Alamofire.request("your image URL in String formate").responseImage { response in
debugPrint(response)
debugPrint(response.result)
if let image = response.result.value {
cell.YourImage.image = image
}
}
return cell
}
Hi I found out the Best solution i.e, through SDWebImages.
* Fist I get the response into url and then append it to a array = (String)[]
* then I called the sdwebimages in cellforitem function...
####CODE
// getting the url images into an array of string
let jsonResponse = jsonObj["response"] as! [[String: Any]]
print("galleryResponse/\(jsonObj)")
for i in 0...jsonResponse.count-1 {
let strGalleryImage = jsonResponse[i]["Gallery_Full"] as? String
print("o12\(strGalleryImage!)")
let str = String((strGalleryImage?.dropFirst(11))!) as? String
print("LAAL\(str)")
if let imgurl = str {
self.arrimages.append(imgurl)
print("kl\(self.arrimages)")
self.collectionView.reloadData()
}
THEN ->
//Calling the images into cellforitem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
// called the url array into sd_setimages
cell.imgProfile.sd_setImage(with: URL(string: arrimages[indexPath.row]), placeholderImage: UIImage(named: "placeholder.jpg"))
return cell
}
Thanks for the answers but this is simplest solution of all...:)