Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIImage缩放纵横比填充和剪裁子视图在UICollectionView中不工作_Ios_Iphone_Uiimageview_Uicollectionview - Fatal编程技术网

Ios UIImage缩放纵横比填充和剪裁子视图在UICollectionView中不工作

Ios UIImage缩放纵横比填充和剪裁子视图在UICollectionView中不工作,ios,iphone,uiimageview,uicollectionview,Ios,Iphone,Uiimageview,Uicollectionview,我有一个UICollectionView,其中一个单元格包含一个UIImageView。我正在将UIImageView frame.size设置为与单元格的frame.size相匹配,并在cellForItemAtIndexPath方法中明确要求UIImageView使用Aspect Fill&Clip子视图进行缩放: let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Ce

我有一个UICollectionView,其中一个单元格包含一个UIImageView。我正在将UIImageView frame.size设置为与单元格的frame.size相匹配,并在cellForItemAtIndexPath方法中明确要求UIImageView使用Aspect Fill&Clip子视图进行缩放:

let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell

... 

var imageView = UIImageView(image: self.eventImages[indexPath.row])
imageView.frame.size = cell.frame.size
cell.contentMode = UIViewContentMode.ScaleAspectFill
cell.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
cell.addSubview(imageView)
结果是一个拉伸图像(图像a),但当我单击(轻触)图像时,它“神奇地”解析了纵横比填充和剪辑子视图(图像B)。我似乎不明白为什么这只是在点击之后才发生,而不是在第一次加载图像时发生。它们以异步方式获取到服务器,但缩放仅在点击图像后发生(也没有实现点击功能)

注意:UICollectionView是在情节提要视图控制器中设置的,集合视图和可重用单元都启用了Aspect Fill和Clip子视图

图像A(缩放错误-拉伸且无裁剪):

图像B(点击后正确缩放):
我认为您应该使用:

imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
与此相反:

  cell.contentMode = UIViewContentMode.ScaleAspectFill
  cell.clipsToBounds = true

很简单,只需执行以下操作:

imageView.contentMode = UIViewContentMode.ScaleAspectFill;
imageView.layer.masksToBounds = YES;

[yourImageView设置内容模式:UIViewContentModeScaleAspectFill];
yourImageView.layer.masksToBounds=是

没有,两者都有,但也只是尝试注释单元格缩放,只留下imageView缩放,但不起作用。在序列图像板中,集合视图和集合可重用单元都具有
方面填充
剪辑子视图
。。。对可能在其中工作的其他内容有任何建议吗?应该是imageView.layer.masksToBounds=YES;你还看到了什么,我写道?Swift 4=
imageView.layer.maskToBounds=true