简体   繁体   English

图像适合UIImageView而无需拉伸

[英]Image fit UIImageView without stretching

In my app, I have upload controller and a feed controller. 在我的应用中,我具有上传控制器和Feed控制器。

In the upload controller the image looks like this: Image Here 在上传控制器中,图像如下所示: Image Here

In the feed controller the image looks like this: Image here 在供应控制图像看起来是这样的: 图片在这里

As you can see, the image is stretched. 如您所见,图像被拉伸了。 Why is that? 这是为什么?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell: CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell

        cell.layer.borderWidth = 0.75
        cell.layer.cornerRadius = 3
        let myColor : UIColor = UIColor(red: 0.0, green: 0.0, blue:0.0, alpha: 0.55)
        cell.backgroundColor = UIColor.whiteColor()
        cell.layer.borderColor = myColor.CGColor

        let post = self.arrayOfDetails[indexPath.row]
        cell.imageText.text = post.text
        cell.uploadedTimeLabel.text = post.CreatedAt.timeAgo

        cell.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "Placeholder"))

        return cell
    }

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        return CGSizeMake(UIScreen.mainScreen().bounds.size.width - 10, 300)
    }

I tried removing the sizeForItemAtIndexPath , that made the image fits good, but the cell does not fits the screen: 我尝试删除sizeForItemAtIndexPath ,使图像适合,但单元格不适合屏幕:

Any suggestions? 有什么建议么?

In your collectionViewCell, if you have made it using storyboard, select the image and set its mode to "AspectFit" 在collectionViewCell中,如果使用情节提要制作了它,请选择图像并将其模式设置为“ AspectFit”

This post may help you to understand about Modes 这篇文章可以帮助您了解模式

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM