【问题标题】:UIImage size in UIImageViewUIImageView 中的 UIImage 大小
【发布时间】:2013-03-05 01:50:59
【问题描述】:

我有一些UIImage 并将其显示在UIImageView 中。

我需要使用UIViewContentModeScaleAspectFit 内容模式。

下面是简单的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];

如何知道屏幕上显示的图像的大小?是否有任何解决方案、属性或我应该手动计算?

编辑:

我知道image.size的房产。

self.image.size ->>> 原图尺寸

imageView.image.size ->>> imageView 的大小,但不是显示的图像。

我根据imageView's size询问显示的大小,它是contentmode

【问题讨论】:

标签: iphone ios objective-c uiimage


【解决方案1】:

这是不可能的。您可以通过以下方法获得原始图像大小

UIImage *image = imageView.image;
CGSize size = image.size;

尺寸

图像的尺寸,考虑到方向。 (只读)

@property(nonatomic, readonly) CGSize size

【讨论】:

    【解决方案2】:

    这是UIImageView 上的一个类别,您可以使用它来根据图像视图上设置的UIViewContentMode 内省显示图像的边界:

    @implementation UIImageView (JRAdditions)
    
    - (CGRect)displayedImageBounds {
        UIImage *image = [self image];
        if(self.contentMode != UIViewContentModeScaleAspectFit || !image)
            return CGRectInfinite;
    
        CGFloat boundsWidth  = [self bounds].size.width,
                boundsHeight = [self bounds].size.height;
    
        CGSize  imageSize  = [image size];
        CGFloat imageRatio = imageSize.width / imageSize.height;
        CGFloat viewRatio  = boundsWidth / boundsHeight;
    
        if(imageRatio < viewRatio) {
            CGFloat scale = boundsHeight / imageSize.height;
            CGFloat width = scale * imageSize.width;
            CGFloat topLeftX = (boundsWidth - width) * 0.5;
            return CGRectMake(topLeftX, 0, width, boundsHeight);
        }
    
        CGFloat scale = boundsWidth / imageSize.width;
        CGFloat height = scale * imageSize.height;
        CGFloat topLeftY = (boundsHeight - height) * 0.5;
    
        return CGRectMake(0, topLeftY, boundsWidth, height);
    }
    
    @end
    

    【讨论】:

      【解决方案3】:

      您也可以通过以下方法获得它:

      -(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);
      
      return CGSizeMake(newwidth, newheight);
      
      }
      

      【讨论】:

        【解决方案4】:

        在swift中,和@jacob-relkin一样

        extension UIImageView {
        
            func displayedImageBounds() -> CGRect {
        
                let boundsWidth = bounds.size.width
                let boundsHeight = bounds.size.height
                let imageSize = image!.size
                let imageRatio = imageSize.width / imageSize.height
                let viewRatio = boundsWidth / boundsHeight
                if ( viewRatio > imageRatio ) {
                    let scale = boundsHeight / imageSize.height
                    let width = scale * imageSize.width
                    let topLeftX = (boundsWidth - width) * 0.5
                    return CGRectMake(topLeftX, 0, width, boundsHeight)
                }
                let scale = boundsWidth / imageSize.width
                let height = scale * imageSize.height
                let topLeftY = (boundsHeight - height) * 0.5
                return CGRectMake(0,topLeftY, boundsWidth,height)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-09-16
          • 1970-01-01
          • 2013-01-16
          • 1970-01-01
          • 1970-01-01
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 2010-09-28
          相关资源
          最近更新 更多