【问题标题】:How to remove spacing from UIImageView aspect fit in Swift如何从 UIImageView 方面删除间距适合 Swift
【发布时间】:2015-09-10 23:08:19
【问题描述】:

我创建了一个UIImageView 并为其分配了一个UIImage。我将 UIImageView 内容模式设置为Aspect Fit。如何从图像的顶部和底部删除填充,或者如何调整 UIImageView 的大小以环绕图像?我什么都试过了。

如果您要说要获取图像的新尺寸,它将无法正常工作。获取UIImageView 中的图像大小仅给出原始图像的宽度和高度。

请不要链接其他帖子。我已经读过了。它们不起作用。

【问题讨论】:

    标签: ios swift uiimageview uiimage


    【解决方案1】:

    您可以手动计算 UIImageView 的帧大小以适合您的图像,或使用 AVMakeRectWithAspectRatioInsideRect(来自 AVFoundation 的方法)。

    【讨论】:

    • 虽然答案可能更详细,但它确实给了我一些解决问题的提示,谢谢!
    • @thibautnoah 你解决了吗?请帮助我
    • @user1629977 我认为我在下面的回答已经足够了,伙计
    【解决方案2】:

    因此,假设您将 UIImageView 设置为与视图相等的宽度。 重新绘制图像以适应的一种方法是使用 Core Graphics。 您所做的是计算重新绘制图像以使其正确适合所需的比例因子。 您可以在这里找到一些很棒的示例:

    https://github.com/natecook1000/Image-Resizing/blob/master/Image%20Resizing/ImageResizingMethods.swift

    还有一个带有基准的好教程,可以更好地理解事物:

    http://nshipster.com/image-resizing/

    我使用的代码示例(基本上是根据我的需要修改的 github 方法之一):

                let image = UIImage(data: data!)
                let oldWidth = image!.size.width
                let scaleFactor = UIWindow().screen.bounds.width / oldWidth
                let cgImage = image!.CGImage
    
                let width = Double(CGImageGetWidth(cgImage)) * Double(scaleFactor)
                let height = Double(CGImageGetHeight(cgImage)) * Double(scaleFactor)
                let bitsPerComponent = CGImageGetBitsPerComponent(cgImage)
                let bytesPerRow = CGImageGetBytesPerRow(cgImage)
                let colorSpace = CGImageGetColorSpace(cgImage)
                let bitmapInfo = CGImageGetBitmapInfo(cgImage)
    
                let context = CGBitmapContextCreate(nil, Int(width), Int(height), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo.rawValue)
    
                CGContextSetInterpolationQuality(context, .High)
    
                CGContextDrawImage(context, CGRect(origin: CGPointZero, size: CGSize(width: CGFloat(width), height: CGFloat(height))), cgImage)
    
                let scaledImage = CGBitmapContextCreateImage(context).flatMap { return UIImage(CGImage: $0) }
                imageView.image = scaledImage
    

    【讨论】:

      【解决方案3】:

      当你使用NSLayoutConstraint时太容易了

          let imageView = UIImageView()
          imageView.contentMode = .scaleAspectFit
          view.addSubview(imageView)
          imageView.translatesAutoresizingMaskIntoConstraints = false
      
          imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
          imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
          imageView.widthAnchor.constraint(equalToConstant: 250).isActive = true
          let heightLayout = imageView.heightAnchor.constraint(equalToConstant: 250)
          heightLayout.isActive = true
      
      
          imageView.image = UIImage(named: "SomeImage")
          if let size = imageView.image?.size {
              heightLayout.constant = size.height / size.width * 250 // 250 is imageView width
              view.layoutIfNeeded()
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 2023-04-10
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多