/*
微信聊天图片适配处理方法
*/
- (void)aspectFitWithImageView:(UIImageView *)imageView {
UIImage *image = imageView.image;
CGFloat imageWidth = CGImageGetWidth(image.CGImage);
CGFloat imageHeight = CGImageGetHeight(image.CGImage);
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
if (imageWidth > viewWidth) {
CGFloat widthRatio = imageWidth / viewWidth;
CGFloat contrastHeight = imageHeight / widthRatio;
if (contrastHeight > viewHeight) {
CGFloat heightRatio = imageHeight / viewHeight;
imageWidth = imageWidth / heightRatio;
imageView.frame = CGRectMake((viewWidth - imageWidth) / 2, 0.0f, imageWidth, viewHeight);
} else {
imageView.frame = CGRectMake(0.0f, (viewHeight - contrastHeight) / 2, viewWidth, contrastHeight);
}
} else {
CGFloat widthRatio = viewWidth / imageWidth;
CGFloat contrastHeight = imageHeight * widthRatio;
if (contrastHeight > viewHeight) {
CGFloat heightRatio = viewHeight / imageHeight;
imageWidth = imageWidth * heightRatio;
imageView.frame = CGRectMake((viewWidth - imageWidth) / 2, 0.0f, imageWidth, viewHeight);
} else {
imageView.frame = CGRectMake(0.0f, (viewHeight - contrastHeight) / 2, viewWidth, contrastHeight);
}
}
}
网友评论