YYWebImage图片处理 —— 尺寸

作者: 天空中的球 | 来源:发表于2016-06-20 21:22 被阅读904次

    在处理图片的过程中,我们常常也会对其大小进行处理,有压缩啊,有直接画啊,可回看下iOS 中压缩图片和截图

    此处我们先看YYImageView是如何处理的:

    - (UIImage *)yy_imageByResizeToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode
    

    简单的直接看实现

    - (UIImage *)yy_imageByResizeToSize:(CGSize)size {
        if (size.width <= 0 || size.height <= 0) return nil;
        UIGraphicsBeginImageContextWithOptions(size, NO, self.scale);
        [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    

    再加上考虑UIViewContentMode的情况下

    - (UIImage *)yy_imageByResizeToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode {
        if (size.width <= 0 || size.height <= 0) return nil;
        UIGraphicsBeginImageContextWithOptions(size, NO, self.scale);
        [self yy_drawInRect:CGRectMake(0, 0, size.width, size.height) withContentMode:contentMode clipsToBounds:NO];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    - (void)yy_drawInRect:(CGRect)rect withContentMode:(UIViewContentMode)contentMode clipsToBounds:(BOOL)clips{
        CGRect drawRect = _YYCGRectFitWithContentMode(rect, self.size, contentMode);
        if (drawRect.size.width == 0 || drawRect.size.height == 0) return;
        if (clips) {
            CGContextRef context = UIGraphicsGetCurrentContext();
            if (context) {
                CGContextSaveGState(context);
                CGContextAddRect(context, rect);
                CGContextClip(context);
                [self drawInRect:drawRect];
                CGContextRestoreGState(context);
            }
        } else {
            [self drawInRect:drawRect];
        }
    }
    

    其中根据尺寸化出来,是核心的,也不难。
    但我们可以了解下 _YYCGRectFitWithContentMode中 mode 的处理

    static CGRect _YYCGRectFitWithContentMode(CGRect rect, CGSize size, UIViewContentMode mode) {
        rect = CGRectStandardize(rect);
        size.width = size.width < 0 ? -size.width : size.width;
        size.height = size.height < 0 ? -size.height : size.height;
        CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
        switch (mode) {
            case UIViewContentModeScaleAspectFit:
            case UIViewContentModeScaleAspectFill: {
                if (rect.size.width < 0.01 || rect.size.height < 0.01 ||
                    size.width < 0.01 || size.height < 0.01) {
                    rect.origin = center;
                    rect.size = CGSizeZero;
                } else {
                    CGFloat scale;
                    if (mode == UIViewContentModeScaleAspectFit) {
                        if (size.width / size.height < rect.size.width / rect.size.height) {
                            scale = rect.size.height / size.height;
                        } else {
                            scale = rect.size.width / size.width;
                        }
                    } else {
                        if (size.width / size.height < rect.size.width / rect.size.height) {
                            scale = rect.size.width / size.width;
                        } else {
                            scale = rect.size.height / size.height;
                        }
                    }
                    size.width *= scale;
                    size.height *= scale;
                    rect.size = size;
                    rect.origin = CGPointMake(center.x - size.width * 0.5, center.y - size.height * 0.5);
                }
            } break;
            case UIViewContentModeCenter: {
                rect.size = size;
                rect.origin = CGPointMake(center.x - size.width * 0.5, center.y - size.height * 0.5);
            } break;
            case UIViewContentModeTop: {
                rect.origin.x = center.x - size.width * 0.5;
                rect.size = size;
            } break;
            case UIViewContentModeBottom: {
                rect.origin.x = center.x - size.width * 0.5;
                rect.origin.y += rect.size.height - size.height;
                rect.size = size;
            } break;
            case UIViewContentModeLeft: {
                rect.origin.y = center.y - size.height * 0.5;
                rect.size = size;
            } break;
            case UIViewContentModeRight: {
                rect.origin.y = center.y - size.height * 0.5;
                rect.origin.x += rect.size.width - size.width;
                rect.size = size;
            } break;
            case UIViewContentModeTopLeft: {
                rect.size = size;
            } break;
            case UIViewContentModeTopRight: {
                rect.origin.x += rect.size.width - size.width;
                rect.size = size;
            } break;
            case UIViewContentModeBottomLeft: {
                rect.origin.y += rect.size.height - size.height;
                rect.size = size;
            } break;
            case UIViewContentModeBottomRight: {
                rect.origin.x += rect.size.width - size.width;
                rect.origin.y += rect.size.height - size.height;
                rect.size = size;
            } break;
            case UIViewContentModeScaleToFill:
            case UIViewContentModeRedraw:
            default: {
                rect = rect;
            }
        }
        return rect;
    }
    
    

    同时也将其 mode 了解了一次

    typedef NS_ENUM(NSInteger, UIViewContentMode) {
        UIViewContentModeScaleToFill, // 拉伸自适应填满整个视图
        UIViewContentModeScaleAspectFit,  // 自适应比例大小显示
        UIViewContentModeScaleAspectFill,    // 原始大小显示
        UIViewContentModeRedraw,     // 尺寸改变时重绘      
        UIViewContentModeCenter,              
        UIViewContentModeTop,
        UIViewContentModeBottom,
        UIViewContentModeLeft,   //中间贴左
        UIViewContentModeRight,  //中间贴右
        UIViewContentModeTopLeft,  // 贴左上
        UIViewContentModeTopRight, // 贴右上
        UIViewContentModeBottomLeft,  // 贴左下
        UIViewContentModeBottomRight,   // 贴右下
    };
    

    此处图文并茂,可以更好的理解,看看这个 UIViewContentMode各模式的含义和效果
    我们在处理图片的时候,可能会出现拉伸变形的情况,根据UIViewContentMode的属性,来控制图片也是很必要的。

    相关文章

      网友评论

      本文标题:YYWebImage图片处理 —— 尺寸

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