【问题标题】:UIImageView aspect fit to heightUIImageView 方面适合高度
【发布时间】:2016-01-13 07:22:10
【问题描述】:

我需要根据高度设置 UIImageView 大小 aspectfit。

意味着我的要求是我的图像高度应该是固定的,但图像的宽度应该根据高度而变化根据高度。

【问题讨论】:

    标签: ios uiimageview uiimage


    【解决方案1】:
    UIImage *originalImage = [UIImage imageNamed:@"image.png"];
    double width = originalImage.size.width;
    double height = originalImage.size.height;
    double apect = width/height;
    
    double nWidth = 320.f/ apect;
    self.img.frame = CGRectMake(0, 0, nWidth, 320.f);
    self.img.center = self.view.center;
    self.img.image = originalImage;
    

    【讨论】:

      【解决方案2】:
      -(CGSize)resizeImage:(CGSize)imageSize toTargetedHeight:(float)height {
      
       NSLog(@"actualImageSize : %@", NSStringFromCGSize(imageSize));
      
       float scaleFactor = height / imageSize.height;
       float newHeight = imageSize.height * scaleFactor;
       float newWidth = imageSize.width  * scaleFactor;
      
       CGSize newSize = CGSizeMake(newWidth, newHeight);
      
       NSLog(@"convertedImageSize : %@", NSStringFromCGSize(newSize));
       return newSize;
      
      }
      

      这将创建具有特定高度的图像并保持其纵横比。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-08
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多