Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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 如何调整UIImageView(swift)的大小_Ios_Iphone_Uiimageview_Swift - Fatal编程技术网

Ios 如何调整UIImageView(swift)的大小

Ios 如何调整UIImageView(swift)的大小,ios,iphone,uiimageview,swift,Ios,Iphone,Uiimageview,Swift,我是swift的初学者,无法调整UIImageView的大小 以下是我当前的布局: 我想要的是调整图像大小以占据屏幕宽度的一半(每行2个图像) 这是我的故事板: 以下是在我的控制器内绘制集合视图的函数: func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! { var c

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

以下是我当前的布局:

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

这是我的故事板:

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

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
    }

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

单元格的大小由您的
UICollectionViewLayout
对象决定,该对象在您的案例中是
UICollectionViewFlowLayout
。如果希望所有单元格大小相同,可以在布局本身上配置属性
itemSize
minimuminermspace
会起作用。或者,如果希望单元格大小不同,例如根据每个单元格包含的图像或其他内容,则需要集合视图委托来实现
UICollectionViewDelegateFlowLayout
方法:

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

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

在本教程之后,您可以调整UIImage的大小,本教程对此进行了详细介绍

在这里,您还可以看到许多关于Swift语言的教程请访问此处

Swift:

func imageResize(imageObj:UIImage, sizeChange:CGSize)-> UIImage {

    let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext() // !!!
    return scaledImage
}

谢谢现在我可以动态调整单元格大小。但我如何调整每个单元格内的图像大小?没问题,@AlexandreNucera。通过调整xib或代码中的各种自动布局约束,设置
UIImageView
autoresizingMask
属性,或在
MenuInspirationCellView
layoutSubviews
函数中手动进行布局,可以调整每个单元格内的图像大小。谢谢,这正是我所需要的。我通过修改约束(删除了固定的120宽度和高度)实现了这一点。
func imageResize(imageObj:UIImage, sizeChange:CGSize)-> UIImage {

    let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext() // !!!
    return scaledImage
}