Ios Swift 3附加到UIImageView
我有一个Ios Swift 3附加到UIImageView,ios,swift,swift3,uiimageview,Ios,Swift,Swift3,Uiimageview,我有一个UIImageView,我想附加来自远程URL 到目前为止,它只适用于一幅图像。 我只是将cell.related.image设置为新图像,并始终将1返回到collectionView 如何切换到附加图像数组 有没有办法在函数中进行追加? 我是否需要始终在collectionView中进行追加 我希望将来在事件侦听器中执行此操作 var img_uls = ["url1" , "url2", "url3"] func append_image(_ path: String, _ cel
UIImageView
,我想附加来自远程URL
到目前为止,它只适用于一幅图像。
我只是将cell.related.image
设置为新图像,并始终将1返回到collectionView
如何切换到附加图像数组
有没有办法在函数中进行追加?
我是否需要始终在collectionView
中进行追加
我希望将来在事件侦听器中执行此操作
var img_uls = ["url1" , "url2", "url3"]
func append_image(_ path: String, _ cell: RelatedCollectionViewCell) {
let url = URL(string: path)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
DispatchQueue.main.async {
cell.related.image = UIImage(data: data!)
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return img_uls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RelatedCollectionViewCell", for: indexPath) as! RelatedCollectionViewCell
append_image(img_uls[indexPath.row], cell);
return cell
}
只需使用包含图像Url的字符串数组,并将其作为
numberOfItems
方法传递,如下所示
var img_uls = ["url1" , "url2", "url3"]
//array of strings containing urls
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return img_uls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RelatedCollectionViewCell", for: indexPath) as! RelatedCollectionViewCell
append_image(img_uls[indexPath.row], cell);
// indexPath.row starts from (0) and goes to (img_uls.count - 1)
return cell
}
现在你必须迭代你的数组img_uls
,这样你就可以从数组中获取Url,获取图像并在collectionView单元格中进行设置。那么,就这样做吧
var img_uls = ["url1" , "url2", "url3"]
//array of strings containing urls
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return img_uls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RelatedCollectionViewCell", for: indexPath) as! RelatedCollectionViewCell
append_image(img_uls[indexPath.row], cell);
// indexPath.row starts from (0) and goes to (img_uls.count - 1)
return cell
}
现在,您需要将图像附加到images
数组中
var images = NSMutableData()
func append_image(_ path: String, _ cell: RelatedCollectionViewCell) {
let url = URL(string: path)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
DispatchQueue.main.async {
images.appendData(data)
cell.related.image = UIImage(data: data!)
}
}
}
您可以使用或,而不是使用自己的append方法来处理该append和所有内容。它们是轻量级的,而且可以管理缓存。因此,您的滚动速度会很快。创建一个字符串的imageUrlArray变量,该变量将保存远程图像URL,然后从
numberOfItemsInSection
方法返回数组的计数。在collectionView:cellForItemAt
函数中,使用imageArray[indexPath.row]从数组中获取要显示的Url,并将Url传递给append\u图像(uu路径:字符串,uu单元:RelatedCollectionViewCell)
函数。更好地使用第三方映像加载库,如或,以更好地处理异步映像下载和缓存
class Example1: UIViewController, UICollectionViewDataSource {
var imageArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
func updateImages(_ images: [String]) {
self.imageArray = images
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RelatedCollectionViewCell", for: indexPath) as! RelatedCollectionViewCell
//append_image(imageArray[indexPath.row], cell)
//Using Kingfisher
let url = imageArray[indexPath.row]
cell.imageView.kf.setImage(with: url, placeholder: nil, options: [.transition(.fade(1))])
return cell
}
}
如何附加到
图像
数组?我的append_image函数接受一个字符串和一个单元格,但调用它就像append_image(images[indexPath.row],cell)
您能否提供新的append\u image
功能?您想在collectionview中填充的整套图像的url在哪里?var img\u uls=[“url1”、“url2”、“url3”]
在视图控件中有几个错误:首先在图像中。append(数据)//追加
i get参数类型“Data?”与所需类型“AnyObject”不符
Then inappend\u image(images[indexPath.row],cell)代码>我得到“AnyObject”不能转换为“String”;你想用“as”吗你能发布一个完整的答案吗?另外,您在任何地方都没有使用图像
数组,只是作为附加的,是否最好使用一个库?你能告诉我如何实现翠鸟吗。我是一个新手,点击链接,查看演示文件夹和文档。如果您以前没有使用过cocoapods,那么也可以访问本文,了解如何在iOS项目中使用第三方库。