如何调整UIImageView(swift)的大小 [英] How to resize an UIImageView (swift)

查看:335
本文介绍了如何调整UIImageView(swift)的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift的初学者,我无法调整UIImageView的大小。

I'm a beginner with swift and I can't get the resize of a UIImageView working.

这是我目前的布局:

我想要的是调整图像大小以占据屏幕宽度的一半(每行2个图像)。

What I want is to resize the images to occupy half the screen's width (2 images per row).

这是我的故事板:

下面是在我的控制器中绘制集合视图的函数:

Here is the function drawing the collection view inside my controller :

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

        cell.imageView.layer.borderWidth = 2
        cell.imageView.layer.borderColor = UIColor.yellowColor().CGColor

        //This is my first approach trying to modify the frame :
        cell.imageView.frame = CGRectMake(0,0, self.view.bounds.width / 2,v120)
        var cellImage : UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string:              images[indexPath.row]))) 
        cell.imageView.image = cellImage;

        //This is my second approach (based on http://www.snip2code.com/Snippet/89236/Resize-Image-in-iOS-Swift) :


        // to resize an image to dimension 52x52
        //var newSize:CGSize = CGSize(width: 52,height: 52)
        //let rect = CGRectMake(0,0, newSize.width, newSize.height)
        //UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        // image is a variable of type UIImage
        //cellImage.drawInRect(rect)
        //let newImage = UIGraphicsGetImageFromCurrentImageContext()
        //UIGraphicsEndImageContext()
        // resized image is stored in constant newImage
        //cell.imageView.image = newImage;


        //This is my thrid approach (based on https://gist.github.com/hcatlin/180e81cd961573e3c54d, of course i added his functions but I don't show them here for the sake of readability) :
         //cell.imageView.image = self.RBSquareImageTo(cellImage, size: CGSize(width: 80, height: 80))

        return cell
    }

有什么可以解决这个问题吗?

Any leads to sort this out ?

推荐答案

单元格的大小由 UICollectionViewLayout 对象决定,该对象是 UICollectionViewFlowLayout 在你的情况下。如果您希望所有单元格大小相同,则可以在布局本身上配置属性; itemSize minimumInterimSpacing 将成功。或者,如果您希望您的单元格大小不同,比如根据每个包含的图像或其他类型,您需要您的集合视图委托来实现 UICollectionViewDelegateFlowLayout 方法:

The sizes of your cells are decided by your UICollectionViewLayout object, which is a UICollectionViewFlowLayout in your case. If you want all of your cells to be the same size, you can configure the properties on the layout itself; itemSize and minimumInterimSpacing will do the trick. Alternatively, if you want your cells to be different sizes, say depending on the image each contains or whatever, you needs your collection view delegate to implement the UICollectionViewDelegateFlowLayout method:

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

返回每个单元格所需的大小。

returning the size you want for each cell.

这篇关于如何调整UIImageView(swift)的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆