検索

ホームページ  >  に質問  >  本文

iOS 图片显示

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

PHP中文网PHP中文网2885日前381

全員に返信(2)返信します

  • 大家讲道理

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

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    返事
    0
  • PHP中文网

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

    これは可能です。
    最初のケース: ローカル イメージを使用する
    ` 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] 画像の幅と高さが正確に印刷されていることがわかります。次に、設定した幅に応じて画像の高さを比例的に拡大縮小できます。 2 番目のケース: ネットワークによってリクエストされた画像、 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);
    }];
    

    `
    ![画像の幅と高さ][2]
    最初のケースと同様に、元の画像の幅と高さがわかったら、独自の幅に比例して高さを拡大縮小できます。

    返事
    0
  • キャンセル返事