【问题标题】:How to get the size of a scaled UIImage in UIImageView?如何在 UIImageView 中获取缩放的 UIImage 的大小?
【发布时间】:2010-09-28 05:00:52
【问题描述】:

UIImageViewimage.size 属性给出了原始UIImage 的大小。我想知道将自动缩放图像放入UIImageView 时的大小(通常小于原始图像)。

例如,我将图像设置为Aspect Fit。现在我想知道它在屏幕上的新高度和宽度,以便在新缩放的图像上准确地绘制。

有没有什么方法可以做到这一点,而无需根据 UIImageView 大小和 UIImage 原始大小自己弄清楚(基本上是对其缩放进行逆向工程)?

【问题讨论】:

  • 遇到了同样的问题,也许我需要自己计算尺寸...

标签: ios objective-c cocoa-touch uiimageview uiimage


【解决方案1】:

从它的框架中获取 UIImageView 的大小怎么样?即 imageView.frame?

【讨论】:

  • image.frame.size 是该视图在其父视图坐标系中的当前大小,可能以像素为单位。
【解决方案2】:

如果在保持图像纵横比的情况下填充框架,则 imageview 框架将与图像大小不同。

【讨论】:

    【解决方案3】:

    由于您已将图像视图设置为 Aspect Fit,您可以简单地获取图像视图的高度和宽度以及原始图像的高度和宽度,然后计算缩放后的图像的高度和宽度。

    也就是说,更好的 方法是使用自定义视图而不是 UIImageView。然后,在自定义视图中自己绘制图像。这样您就可以控制它的各个方面。

    【讨论】:

    • 感谢您说出“更好的方法”。我试图忍受以适合方面的故事板方式进行操作,但是尝试以这种方式正确处理一切非常痛苦,手动操作在各方面都更好。
    【解决方案4】:

    目标-C:

    -(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
    {
        float imageRatio = image.size.width / image.size.height;
        float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
        if(imageRatio < viewRatio)
        {
            float scale = imageView.frame.size.height / image.size.height;
            float width = scale * image.size.width;
            float topLeftX = (imageView.frame.size.width - width) * 0.5;
            return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
        }
        else
        {
            float scale = imageView.frame.size.width / image.size.width;
            float height = scale * image.size.height;
            float topLeftY = (imageView.frame.size.height - height) * 0.5;
    
            return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
        }
    }
    

    斯威夫特 4:

    func frame(for image: UIImage, inImageViewAspectFit imageView: UIImageView) -> CGRect {
      let imageRatio = (image.size.width / image.size.height)
      let viewRatio = imageView.frame.size.width / imageView.frame.size.height
      if imageRatio < viewRatio {
        let scale = imageView.frame.size.height / image.size.height
        let width = scale * image.size.width
        let topLeftX = (imageView.frame.size.width - width) * 0.5
        return CGRect(x: topLeftX, y: 0, width: width, height: imageView.frame.size.height)
      } else {
        let scale = imageView.frame.size.width / image.size.width
        let height = scale * image.size.height
        let topLeftY = (imageView.frame.size.height - height) * 0.5
        return CGRect(x: 0.0, y: topLeftY, width: imageView.frame.size.width, height: height)
      }
    }
    

    【讨论】:

    • 如果传入的图像可以包含 0 高度或 0 宽度,则生成的 CGRect 可能包含 NaN。如果发生这种情况要小心。
    • 这个答案让我很开心。谢谢! :-D
    • 这是一个完美的解决方案...很棒的想法..!!一切顺利,谢谢。
    • 你是我这一天的救星。谢谢 ! :)
    • 完全符合我的预期。谢谢
    【解决方案5】:

    这个简单的函数将计算宽高比拟合后的图像大小:

    -(CGSize)imageSizeAfterAspectFit:(UIImageView*)imgview{
    
    
        float newwidth;
        float newheight;
    
        UIImage *image=imgview.image;
    
        if (image.size.height>=image.size.width){
            newheight=imgview.frame.size.height;
            newwidth=(image.size.width/image.size.height)*newheight;
    
            if(newwidth>imgview.frame.size.width){
                float diff=imgview.frame.size.width-newwidth;
                newheight=newheight+diff/newheight*newheight;
                newwidth=imgview.frame.size.width;
            }
    
        }
        else{
            newwidth=imgview.frame.size.width;
            newheight=(image.size.height/image.size.width)*newwidth;
    
            if(newheight>imgview.frame.size.height){
                float diff=imgview.frame.size.height-newheight;
                newwidth=newwidth+diff/newwidth*newwidth;
                newheight=imgview.frame.size.height;
            }
        }
    
        NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight);
    
    
        //adapt UIImageView size to image size
        //imgview.frame=CGRectMake(imgview.frame.origin.x+(imgview.frame.size.width-newwidth)/2,imgview.frame.origin.y+(imgview.frame.size.height-newheight)/2,newwidth,newheight);
    
        return CGSizeMake(newwidth, newheight);
    
    }
    

    【讨论】:

      【解决方案6】:

      这个简单的函数将计算图像的大小:-

      [imageView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame)];
      

      更多详情,您可以参考AVMakeRectWithAspectRatioInsideRect - AVFoundation

      【讨论】:

      • 这绝对是最好的答案!我不知道这个功能,非常感谢;)
      【解决方案7】:

      cncool answer 的改编,作为 UIImageView 的扩展,也许对某人有用:

      extension UIImageView{
          func frameForImageInImageViewAspectFit() -> CGRect
          {
              if  let img = self.image {
                  let imageRatio = img.size.width / img.size.height;
      
                  let viewRatio = self.frame.size.width / self.frame.size.height;
      
                  if(imageRatio < viewRatio)
                  {
                      let scale = self.frame.size.height / img.size.height;
      
                      let width = scale * img.size.width;
      
                      let topLeftX = (self.frame.size.width - width) * 0.5;
      
                      return CGRectMake(topLeftX, 0, width, self.frame.size.height);
                  }
                  else
                  {
                      let scale = self.frame.size.width / img.size.width;
      
                      let height = scale * img.size.height;
      
                      let topLeftY = (self.frame.size.height - height) * 0.5;
      
                      return CGRectMake(0, topLeftY, self.frame.size.width, height);
                  }
              }
      
              return CGRectMake(0, 0, 0, 0)
          }
      }
      

      【讨论】:

        【解决方案8】:

        跟进@MeetDoshi,这里有一个可以适应这个的扩展。

        extension UIImageView {
            /// Retrieve the scaled size of the image within this ImageView.
            /// - Returns: A CGRect representing the size of the image after scaling or nil if no image is set.
            func getScaledImageSize() -> CGRect? {
                if let image = self.image {
                    return AVMakeRect(aspectRatio: image.size, insideRect: self.frame);
                }
        
                return nil;
            }
        }
        

        【讨论】:

          【解决方案9】:

          我编写了一个有用的扩展,其中包括它的一组附加功能,可能值得一试。

          包含的功能

          func getScaledImageSize() -> CGRect?
          func getScaledImageSizeWithPadding() -> CGRect?
          func getPaddingTop() -> CGFloat? {
          func getPaddingBottom() -> CGFloat? {
          func getPaddingLeft() -> CGFloat? {
          func getPaddingRight() -> CGFloat?
          func getScaledCoordinate(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?
          func getScaledCoordinateWithPadding(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?
          

          https://gist.github.com/nathan-fiscaletti/e1b85f68559f305db7342bfff9574563

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-16
            • 1970-01-01
            相关资源
            最近更新 更多