问题描述
我正在使用集合视图,它包含一个imageview。图片采用url格式,因此我编写了一个将url转换为Image的功能,但是图片显示不正确。 我尝试了所有模式,例如长宽比适合,长宽比填充,缩放以填充。 我在互联网上进行了很多搜索,但没有找到答案。
这是我的代码:
extension HomeDemoViewController : UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return bannerArray.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeAddCollectionViewCell",for: indexPath)
as! HomeAddCollectionViewCell
cell.imageview.setImageFromURl(stringImageUrl: imageArray[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
self.navigationController!.pushViewController(vc,animated: true)
}
}
func setImageFromURl(stringImageUrl url: String){
if let url = NSURL(string: url) {
DispatchQueue.global(qos: .default).async{
if let data = NSData(contentsOf: url as URL) {
DispatchQueue.main.async {
self.image = UIImage(data: data as Data)
}
}
}
}
}
解决方法
请描述您的问题,并附上屏幕截图。 似乎问题出在图像上,请尝试设置其他内容模式,如果需要,请尝试-clipsToBounds = true