给imageView如何设置约束才能让他显示图片原来正确的比例(不是固定宽度,然后根据图片比例,设置宽高比例约束等比缩放那种),,,图片的比例事先未知,,可以做到吗?
认证高级PHP讲师
imageView.contentMode = UIViewContentModeScaleAspectFit;
これは可能です。 最初のケース: ローカル イメージを使用する ` 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] 最初のケースと同様に、元の画像の幅と高さがわかったら、独自の幅に比例して高さを拡大縮小できます。
これは可能です。
最初のケース: ローカル イメージを使用する
` UIImageView *fenxiangImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fenxiang"]];
NSLogFloat(fenxiangImageView.frameSizeWidth);
NSLogFloat(fenxiangImageView.frameSizeHeight);
[self.view addSubview:fenxiangImageView];
![画像の説明][1] 画像の幅と高さが正確に印刷されていることがわかります。次に、設定した幅に応じて画像の高さを比例的に拡大縮小できます。 2 番目のケース: ネットワークによってリクエストされた画像、 UIImageView *urlImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"]]]];
NSLogFloat(urlImageView.frameSizeWidth);
NSLogFloat(urlImageView.frameSizeHeight);
[self.view addSubview:urlImageView];
`
![画像の幅と高さ][2]
最初のケースと同様に、元の画像の幅と高さがわかったら、独自の幅に比例して高さを拡大縮小できます。