【问题标题】:How can I get the height and width of an uiimage?如何获取uiimage的高度和宽度?
【发布时间】:2011-07-12 02:28:14
【问题描述】:

来自网址Image in Mail

我正在将图像添加到邮件视图。它将显示完整图像。但我想计算,按比例改变图像的高度和宽度。

如何获取UIImage的高度和宽度?

【问题讨论】:

标签: ios uiimage cgsize


【解决方案1】:
let heightInPoints = image.size.height
let heightInPixels = heightInPoints * image.scale

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale

【讨论】:

  • 积分。乘以 scale 属性得到像素。
  • 这很奇怪。我用相机拍了一张照片,并假设它的高度>宽度。但是宽度点是 1280,高度点是 720。它们有什么问题?
  • 从相机胶卷(通过 UIImagePickerController)加载图像时,比例似乎总是 1.0,但 image.size 不是 image.size 不是以像素为单位给出的。 (它实际上是像素大小的一半)。 -> 如何获得正确的像素大小 - 不能使用 UIScreen.main.scale 因为在 iPhone 6/7 Plus 上是 3,但 image.size 保持不变
  • 好像image.scale默认只有1.0,前面的评论有提到。话虽如此,我得到的默认模拟器图像的值类似于 (3000, 2002),所以它必须是实际像素而不是点值,对吧?这篇文章已经有几年了,所以也许 image.size 已经改变以反映实际的像素值。
【解决方案2】:

在 UIImage 实例上使用 size 属性。详情请见the documentation

【讨论】:

    【解决方案3】:
    UIImage *img = [UIImage imageNamed:@"logo.png"];
    
    CGFloat width = img.size.width;
    CGFloat height = img.size.height;
    

    【讨论】:

      【解决方案4】:

      上面的一些anwsers,在旋转设备时效果不佳。

      试试:

      CGRect imageContent = self.myUIImage.bounds;
      CGFloat imageWidth = imageContent.size.width;
      CGFloat imageHeight = imageContent.size.height;
      

      【讨论】:

        【解决方案5】:
        UIImageView *imageView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MyImage.png"]]autorelease];
        
        NSLog(@"Size of my Image => %f, %f ", [[imageView image] size].width, [[imageView image] size].height) ;
        

        【讨论】:

          【解决方案6】:

          那里有很多有用的解决方案,但没有简化的扩展方法。这是解决扩展问题的代码:

          extension UIImage {
          
              var getWidth: CGFloat {
                  get {
                      let width = self.size.width
                      return width
                  }
              }
          
              var getHeight: CGFloat {
                  get {
                      let height = self.size.height
                      return height
                  }
              }
          }
          

          【讨论】:

            【解决方案7】:
                import func AVFoundation.AVMakeRect
            
                let imageRect = AVMakeRect(aspectRatio: self.image!.size, insideRect: self.bounds)
            
                x = imageRect.minX
            
                y = imageRect.minY
            

            【讨论】:

            • 从 UIImage 使用它比使用其他框架简单。
            【解决方案8】:
            let imageView: UIImageView = //this is your existing imageView
            
            let imageViewHeight: CGFloat = imageView.frame.height
            
            let imageViewWidth: CGFloat = imageView.frame.width
            

            【讨论】:

            • 你展示了如何获取imageView的大小,它与图像的大小不一样
            猜你喜欢
            • 2014-05-01
            • 1970-01-01
            • 2019-12-26
            • 2011-09-25
            • 2012-10-10
            • 2014-07-27
            • 1970-01-01
            • 2011-05-31
            • 2012-05-20
            相关资源
            最近更新 更多