iOS 图片显示
PHP中文网
PHP中文网 2017-04-17 14:48:25
0
2
363

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

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
大家讲道理
imageView.contentMode = UIViewContentModeScaleAspectFit;
Ty80

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template