Skip to content

Instantly share code, notes, and snippets.

@mario1in
Created February 10, 2017 08:22
Show Gist options
  • Select an option

  • Save mario1in/268f5077c9b751f12656224c6a973978 to your computer and use it in GitHub Desktop.

Select an option

Save mario1in/268f5077c9b751f12656224c6a973978 to your computer and use it in GitHub Desktop.
图片居于顶部对齐显示
CGSize imageSize = image.size;
if (imageSize.height > imageSize.width) {
weakSelf.imageView.contentMode = UIViewContentModeScaleAspectFit;
weakSelf.imageView.image = image;
return;
}
CGFloat imageViewOriginX = 0;
CGFloat imageViewOriginY = 0;
CGFloat imageViewWidth;
CGFloat imageViewHeight;
CGFloat widthScaleFactor = CGRectGetWidth(weakSelf.frame) / imageSize.width;
CGFloat heightScaleFactor = CGRectGetHeight(weakSelf.frame) / imageSize.height;
// 计算imageView的缩放大小
if (widthScaleFactor < heightScaleFactor) {
imageViewWidth = weakSelf.bounds.size.width;
imageViewHeight = weakSelf.bounds.size.width * (imageSize.height / imageSize.width);
} else {
imageViewWidth = weakSelf.bounds.size.height * (imageSize.width / imageSize.height);
imageViewHeight = weakSelf.bounds.size.height;
}
// 计算x
imageViewOriginX += (weakSelf.frame.size.width - imageViewWidth)/2;
// 计算y
if (imageSize.width >= imageSize.height) {
imageViewOriginY += 64.f;
}
[weakSelf.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(weakSelf).mas_offset(imageViewOriginY);
make.left.mas_equalTo(weakSelf).mas_offset(imageViewOriginX);
make.width.mas_equalTo(imageViewWidth);
make.height.mas_equalTo(imageViewHeight);
}];
[weakSelf layoutIfNeeded];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment