美文网首页
iOS 微信聊天图片适配处理方法

iOS 微信聊天图片适配处理方法

作者: 奔跑吧小蚂蚁 | 来源:发表于2019-12-10 00:33 被阅读0次
 /*
  微信聊天图片适配处理方法
  */
- (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);
        }
    }
}

相关文章

网友评论

      本文标题:iOS 微信聊天图片适配处理方法

      本文链接:https://www.haomeiwen.com/subject/ockwgctx.html