【问题标题】:How to resize an UIImageView (swift)如何调整 UIImageView 的大小(swift)
【发布时间】:2014-10-22 04:19:48
【问题描述】:

我是 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
    }

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

【问题讨论】:

    标签: ios iphone uiimageview swift


    【解决方案1】:

    单元格的大小由您的UICollectionViewLayout 对象决定,在您的情况下为UICollectionViewFlowLayout。如果您希望所有单元格的大小相同,则可以在布局本身上配置属性; itemSizeminimumInterimSpacing 可以解决问题。或者,如果您希望您的单元格具有不同的大小,例如根据每个包含的图像或其他内容,您需要您的集合视图委托来实现 UICollectionViewDelegateFlowLayout 方法:

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

    为每个单元格返回您想要的大小。

    【讨论】:

    • 谢谢!现在我可以动态调整单元格的大小。但是如何调整每个单元格内的图像大小?
    • 没问题,@AlexandreNucera。每个单元格内的图像可以通过调整 xib 或代码中的各种自动布局约束、设置UIImageViewautoresizingMask 属性或在MenuInspirationCellViewlayoutSubviews 函数中手动进行布局来调整大小。
    • 谢谢,这正是我所需要的。我通过修改我的约束来完成这项工作(我删除了固定的 120 宽度和高度)。
    【解决方案2】:

    按照本教程,您可以调整 UIImage 的大小,在该教程中进行了全面描述.. Image Resize in swift ios8

        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()
        return scaledImage
    }
    

    在这里您还可以看到许多关于 Swift 语言的教程 访问这里iPhone & iPad Application Development Help World

    【讨论】:

      【解决方案3】:

      斯威夫特:

      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
      }
      

      【讨论】:

        猜你喜欢
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        • 2020-06-17
        • 1970-01-01
        相关资源
        最近更新 更多