图片处理方法(截取、马赛克、拉伸)

图片拉伸处理

实现类似Android中点9图的效果,拉伸图片不造成变形且不降低图片清晰度.

图片拉伸

主要使用image的resizableImageWithCapInsets方法实现,设置你想要保留的图片边缘大小,保持边缘不变,将中间部分进行拉伸。

/**设置图片不会变形拉伸*/
+ (UIImage *)stretchImage:(UIImage *)image edgeInsets:(UIEdgeInsets)insets
{
    //CGFloat top; // 顶端盖高度
    //CGFloat bottom; // 底端盖高度
    //CGFloat left; // 左端盖宽度
    //CGFloat right; // 右端盖宽度
    //UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    // 指定为拉伸模式,伸缩后重新赋值
    image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    return image;
}

截取图片

截取图片的一部分


截取图片

矩形区域截取

该方法是截取图片的矩形区域,给矩形的位置大小就可以了。

+ (UIImage *)cropImage:(UIImage *)image cropRect:(CGRect)rect
{
    if(!image){
        return nil;
    }
    
    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, subImageRef);
    UIImage *cropImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    CGImageRelease(subImageRef);
    return cropImage;
}

指定区域截取

通过一系列的关键点来控制要需要截取的图片区域,使用path来设置剪裁区域。

+ (UIImage *)cropImage:(UIImage *)image pointArray:(NSArray *)pointArr
{
    if (pointArr.count == 0 || !image) {
        return nil;
    }
    
    CGPoint *points = malloc(sizeof(CGPoint) * pointArr.count);
    for (int i = 0; i < pointArr.count; ++i) {
        points[i] = [[pointArr objectAtIndex:i] CGPointValue];
    }
    
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * image.scale, image.size.height * image.scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(context);
    CGContextAddLines(context, points, pointArr.count);
    CGContextClosePath(context);
    CGRect boundsRect = CGContextGetPathBoundingBox(context);
    UIGraphicsEndImageContext();
    
    UIGraphicsBeginImageContext(boundsRect.size);
    context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, CGRectMake(0, 0, boundsRect.size.width, boundsRect.size.height));
    
    CGMutablePathRef  path = CGPathCreateMutable();
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-boundsRect.origin.x, -boundsRect.origin.y);
    CGPathAddLines(path, &transform, points, pointArr.count);
    
    CGContextBeginPath(context);
    CGContextAddPath(context, path);
    CGContextClip(context);
    
    [image drawInRect:CGRectMake(-boundsRect.origin.x, -boundsRect.origin.y, image.size.width * image.scale, image.size.height * image.scale)];
    
    UIImage *cropImage = UIGraphicsGetImageFromCurrentImageContext();
    
    CGPathRelease(path);
    UIGraphicsEndImageContext();
    
    return cropImage;
}

给图片打马赛克

打码图片

马赛克的主要算法

unsigned char *pixels[4] = {0};
if (i % sizeLevel == 0) {
   if (j % sizeLevel == 0) {
        memcpy(pixels, bitMapData+4*currentIndex, 4);
   }else{
        memcpy(bitMapData+4*currentIndex, pixels, 4);
   }
}else{
   preCurrentIndex = (i-1)*imageW+j;
   memcpy(bitMapData+4*currentIndex, bitMapData+4*preCurrentIndex, 4);
}

拖动打码实现

使用两张图片来实现,原图放底部,马赛克处理过的图片在上层,用layer来控制区域的显示,具体实现可查看demo。

self.imageLayer = [CALayer layer];
self.imageLayer.frame = self.bounds;
[self.layer addSublayer:self.imageLayer];
        
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = self.bounds;
self.shapeLayer.lineCap = kCALineCapRound;
self.shapeLayer.lineJoin = kCALineJoinRound;
self.shapeLayer.lineWidth = 20;
self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
self.shapeLayer.fillColor = nil;//此处必须设为nil,否则后边添加addLine的时候会自动填充

self.imageLayer.mask = self.shapeLayer;
self.path = CGPathCreateMutable();

图片圆角处理

通过Quartz2D裁剪图片

/**圆形图片*/
+ (UIImage *)circleImage:(UIImage *)image
{
    if(!image){
        return nil;
    }

    UIGraphicsBeginImageContext(image.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    CGContextClip(ctx);
    [image drawInRect:rect];
    UIImage *cicleImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return cicleImage;
}

Demo

demo

参考

马赛克算法

你可能感兴趣的:(图片处理方法(截取、马赛克、拉伸))