gpt4 book ai didi

swift - 如何使用 sdwebimage 框架 swift 将图像从 url 传递到另一个 View Controller

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

我在第一个 View Controller 中有一个 Collection View 单元,其中使用 sd webimage 第三方库从 url 获取 imageview。此图像是缩略图。我想将实际图像传递到第一个 View Controller 的 didselectitemat 中的另一个 View Controller 代码如下:

 func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return subcategoryArray.count

}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

print("inside cell for item at ")

let cell:SubCategoryCollectionViewCell = self.collectionview3.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! SubCategoryCollectionViewCell

if defaultAnimalArray.count - 1 >= indexPath.row
{
let item = self.defaultAnimalArray[indexPath.row]
cell.subThumbImg?.image = UIImage(named: item as! String)
}

else
{
//If now defaultAnimalArray.count = 8, indexPath = 8 , But array = 0...4, then,
let item1 = self.subcategoryArray[indexPath.row - self.defaultAnimalArray.count]
self.subcatthumbimagelink = (item1 as AnyObject).object(forKey: "SubcategoryThumb") as! String
cell.subThumbImg.sd_setImage(with: URL(string: self.subcatthumbimagelink), placeholderImage: UIImage(named: "placeholder.png"),options: SDWebImageOptions(), completed: {(image, error, cacheType, imageURL) -> Void in
print("image loaded")
})
}


return cell

}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("image selected for coloring")


if defaultAnimalArray.count - 1 >= indexPath.row
{
print("indexpath selected is \(indexPath.row)")
let item = self.animalcategoryImages[indexPath.row]
var drawVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawingViewController") as! DrawingViewController
drawVC.selectedimage = UIImage(named:item)
self.navigationController?.pushViewController(drawVC, animated: true)
}

else
{
print("indexpath selected in else loop is \(indexPath.row)")


let item1 = self.subcategoryArray[indexPath.row - 10]
print("subcategory count after manipulation is \(self.subcategoryArray)")
print("count of item1 \((item1 as AnyObject).count)")
print("item1 is \(item1)")
self.subcategoryimagelink = (item1 as AnyObject).object(forKey: "SubcategoryImage") as! String
print("category image link is \(self.subcategoryimagelink)")
self.ImageviewMain.sd_setImage(with: URL(string: self.subcategoryimagelink))

var drawVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawingViewController") as! DrawingViewController
drawVC.selectedimage = self.ImageviewMain.image
self.navigationController?.pushViewController(drawVC, animated: true)

}

子类别计数是 url 中的数组和默认数组的相加。请帮助我如何将单元格中的 url 中的图像传递到另一个 View Controller 。

最佳答案

您只需传递图像url字符串,而不是UIImage对象

var drawVC  = self.storyboard?.instantiateViewController(withIdentifier: "DrawingViewController") as! DrawingViewController
drawVC.imageURL = self.subcatthumbimagelink // you can also pass string from array
self.navigationController?.pushViewController(drawVC, animated: true)

在DrawingViewController中

 var imageURL : String?
self.imageView.sd_setImage(with: URL(string:imageURL), placeholderImage: UIImage(named: "placeholder.png"),options: SDWebImageOptions(), completed: {(image, error, cacheType, imageURL) -> Void in
print("image loaded")
})

关于swift - 如何使用 sdwebimage 框架 swift 将图像从 url 传递到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149523/

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