【问题标题】:Get width of a resized image after UIViewContentModeScaleAspectFit在 UIViewContentModeScaleAspectFit 之后获取调整大小的图像的宽度
【发布时间】:2013-01-16 20:37:32
【问题描述】:

我有我的 UIImageView,我在其中放了一张我这样调整大小的图片:

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
attachmentImageNew.image = actualImage;
attachmentImageNew.backgroundColor = [UIColor redColor];
attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;

我尝试通过这样做在我的UIImageView 中获取调整后图片的宽度:

NSLog(@"Size of pic is %f", attachmentImageNew.image.size.width);

但它实际上返回了原始图片的宽度。关于如何获得我在屏幕上看到的图片框架的任何想法?

编辑:这是我的UIImageView 的样子,红色区域是它的backgroundColor

【问题讨论】:

  • 只需检查我的答案中的更新。

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


【解决方案1】:

我不知道是否有更明确的解决方案,但这可行:

float widthRatio = imageView.bounds.size.width / imageView.image.size.width;
float heightRatio = imageView.bounds.size.height / imageView.image.size.height;
float scale = MIN(widthRatio, heightRatio);
float imageWidth = scale * imageView.image.size.width;
float imageHeight = scale * imageView.image.size.height;

【讨论】:

  • 对于那些想知道MIN()函数是什么的人,我相信你可以用这行代码替换:float scale = widthRatio
  • 快速复制粘贴功能,与@Mikhail的回答有关:func getScaledSizeOfImage(image: UIImage, toSize: CGSize) -> CGSize { var widthRatio = toSize.width/image.size.width var heightRatio = toSize.height/image.size.height var scale = min(widthRatio, heightRatio) var imageWidth = scale*image.size.width var imageHeight = scale*image.size.height return CGSizeMake(imageWidth, imageHeight) }
  • Swift 3.0 版本:让 widthRatio = imageView.bounds.size.width / (imageView.image?.size.width)!让 heightRatio = imageView.bounds.size.height / (imageView.image?.size.height)!让 scale = min(widthRatio, heightRatio) 让 imageWidth = scale * (imageView.image?.size.width)!让 imageHeight = scale * (imageView.image?.size.height)!
【解决方案2】:

Swift 中的解决方案:

let currentHeight = imageView.bounds.size.height
let currentWidth = imageView.bounds.size.width
let newWidth = UIScreen.mainScreen().bounds.width
let newHeight = (newWidth * currentHeight) / currentWidth

println(newHeight)

【讨论】:

    【解决方案3】:

    它具有原始图像的参考,因此始终提供与原始图像相同的尺寸。

    要获得新图像的尺寸,您必须检查纵横比。我已经使用预览导出了一个公式来满足我的需要,它使用不同大小的不同图像,它如何根据图像的纵横比调整图像大小。

    【讨论】:

      【解决方案4】:

      根据 Apple 的 UIViewContentModeScaleAspectFit 文档

      它通过保持 纵横比。

      这意味着您的图像尺寸(缩放后)应与您的UIImageView 相同。

      更新:

      如果您不想使用UIViewContentModeScaleAspectFit,给您一个新建议,如果您可以修复scaledImage 的大小,那么您可以使用下面的代码来缩放图像以修复 newSize,然后您可以使用宽度到你的代码。

      CGSize newSize = CGSizeMake(100.0,50.0);
      UIGraphicsBeginImageContext( newSize );
      [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
      UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      

      【讨论】:

        【解决方案5】:

        在你的情况下:

        float yourScaledImageWitdh =  attachmentImageNew.frame.size.height *  attachmentImageNew.image.size.width / attachmentImageNew.image.size.height
        NSLog(@"width of resized pic is %f", yourScaledImageWitdh);
        

        但你也应该检查一下,之前,原始图像比例与 imageView 比例,我的代码行是好的,以防红色区域水平添加,而不是你的原始图像比例比 imageView 帧比例宽

        【讨论】:

          【解决方案6】:

          对于 Swift 2,您可以使用此代码 sn-p 将 aspectFitted 图像与 屏幕的 底部 对齐(取自早先对这个问题的回答——>谢谢你们,伙计们)

          let screenSize: CGRect = UIScreen.mainScreen().bounds
          let screenWidth = CGFloat(screenSize.width)
          let screenHeight = CGFloat(screenSize.height)
          
          imageView.frame = CGRectMake(0, screenHeight - (imageView.image?.size.height)! , screenWidth, (imageView.image?.size.height)!)
          
          let newSize:CGSize = getScaledSizeOfImage(imageView.image!, toSize: self.view.frame.size)
          
          imageView.frame = CGRectMake(0, screenHeight - (newSize.height) , screenWidth, newSize.height)
          
          func getScaledSizeOfImage(image: UIImage, toSize: CGSize) -> CGSize       
          {
              let widthRatio = toSize.width/image.size.width
              let heightRatio = toSize.height/image.size.height
              let scale = min(widthRatio, heightRatio)
              let imageWidth = scale*image.size.width
              let imageHeight = scale*image.size.height
              return CGSizeMake(imageWidth, imageHeight)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-25
            • 2013-04-28
            • 2013-09-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多