search

Home  >  Q&A  >  body text

iOS 图片显示

给imageView如何设置约束才能让他显示图片原来正确的比例(不是固定宽度,然后根据图片比例,设置宽高比例约束等比缩放那种),,,图片的比例事先未知,,可以做到吗?

PHP中文网PHP中文网2886 days ago385

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 14:50:25

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:50:25

    This is possible.
    The first case: use local images,
    ` UIImageView *fenxiangImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fenxiang"]];
    NSLogFloat(fenxiangImageView.frameSizeWidth);
    NSLogFloat(fenxiangImageView.frameSizeHeight);
    [self.view addSubview:fenxiangImageView];

    [fenxiangImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15);
        make.top.equalTo(self.view.mas_top).offset(100);
    }];
    

    ![图片描述][1] 可以看到图片宽高准确的打印出来了。然后你可以根据自己定的宽度,来等比缩放图片的高度就行了。 第二种情况:网络请求的图片, UIImageView *urlImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"]]]];
    NSLogFloat(urlImageView.frameSizeWidth);
    NSLogFloat(urlImageView.frameSizeHeight);
    [self.view addSubview:urlImageView];

    [urlImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15);
        make.top.equalTo(self.view.mas_top).offset(100);
    }];
    

    `
    ![Picture width and height][2]
    As in the first case, once you know the width and height of the original image, you can scale the height in proportion to your own width.

    reply
    0
  • Cancelreply