Ios 如何在单元格中为集合视图的索引路径函数调用图像URL和图像视图
我想在集合视图中异步调度8个图像URL。我已经为集合视图单元创建了一个类,并在其中创建了imageview的出口。现在我想从主视图控制器配置imageview。这是密码Ios 如何在单元格中为集合视图的索引路径函数调用图像URL和图像视图,ios,swift,Ios,Swift,我想在集合视图中异步调度8个图像URL。我已经为集合视图单元创建了一个类,并在其中创建了imageview的出口。现在我想从主视图控制器配置imageview。这是密码 let reuseIdentifier = "PhotosCollectionViewCell" // also enter this string as the cell identifier in the storyboard var items = ["1", "2", "3", "4", "5", "6",
let reuseIdentifier = "PhotosCollectionViewCell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5", "6", "7", "8"]
// tell the collection view how many cells to make
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotosCollectionViewCell
cell.imageView = imageView
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
func loadImage() {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
let urlString = "http://charcoaldesign.co.uk/AsyncImageView/Forest/IMG_0352.JPG"
let url = NSURL(string: urlString)
let data = NSData(contentsOfURL: url!)
dispatch_async(dispatch_get_main_queue(), {
self.imageView.image = UIImage(data: data!)
// self.items[0] = (data as? String)!
})
}
}
}
这里有一个扩展,使事情变得更简单
extension UIImageView {
func downloadImageFrom(link link:String, contentMode: UIViewContentMode) {
NSURLSession.sharedSession().dataTaskWithURL( NSURL(string:link)!, completionHandler: {
(data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue()) {
self.contentMode = contentMode
if let data = data { self.image = UIImage(data: data) }
}
}).resume()
}
}
cell.imageView.downloadImageFrom(link: imageLinkArray[indexPath.row], contentMode: UIViewContentMode.ScaleAspectFit) //set your image from link array.
您也可以在下面的url中查找更多帮助。谢谢您的回复。这里有8个图像URL。我如何运行循环,以便在集合视图中显示8个图像?请按如下方式将imageurl与imageView一起显示,以便根据您的collectionViewCell动态调用它。loadImage(imageView,url),func loadImage(imageViewObj:UIImageView,url:NSString)我遇到很多错误,请给我一个详细的解决方案在集合视图中使用UIImageView+WebCache处理异步图像
extension UIImageView {
func downloadImageFrom(link link:String, contentMode: UIViewContentMode) {
NSURLSession.sharedSession().dataTaskWithURL( NSURL(string:link)!, completionHandler: {
(data, response, error) -> Void in
dispatch_async(dispatch_get_main_queue()) {
self.contentMode = contentMode
if let data = data { self.image = UIImage(data: data) }
}
}).resume()
}
}
cell.imageView.downloadImageFrom(link: imageLinkArray[indexPath.row], contentMode: UIViewContentMode.ScaleAspectFit) //set your image from link array.