0x00 原理

利用一张图片事先画好的图片(以下称为蒙板),盖在要被裁剪的的图片上,然后遍历蒙板上的像素点,修改被裁剪图片对应位置的像素的色值即可得到一些我们想要的不规则图片了(比如人脸)

0x01 代码实现(UIImage分类)

#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
#define A(x) ( Mask8(x >> 24) )
#define RGBAMake(r, g, b, a) ( Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24 )
- (UIImage *)processWithMaskImage:(UIImage *)maskImg {
if (self == nil) {
return nil;
}
// 1. Get the raw pixels of the image
UInt32 *inputPixels;
UInt32 *maskPixels; CGImageRef maskCGImage = [maskImg CGImage]; //蒙板图片
CGImageRef inputCGImage = [self CGImage];//准备被裁剪的图片
NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
NSUInteger inputHeight = CGImageGetHeight(inputCGImage); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8; NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth; inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));
maskPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32)); //被裁剪图片的上下文
CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
bitsPerComponent, inputBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage); //蒙板图片的上下文
CGContextRef maskContext = CGBitmapContextCreate(maskPixels, inputWidth, inputHeight,
bitsPerComponent, inputBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(maskContext, CGRectMake(0, 0, inputWidth, inputHeight), maskCGImage); // 3. Convert the image
for (NSUInteger j = 0; j < inputHeight; j++) {
for (NSUInteger i = 0; i < inputWidth; i++) {
//获得源图片和蒙板图片每一个像素值
UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;
UInt32 * currentMaskPixel = maskPixels + (j * inputWidth) + i;
UInt32 color = *currentMaskPixel;
NSInteger alpha = A(color); //获得蒙板上当前像素的alpha值
if (alpha != 0) { //如果不是透明,就修改为透明
//修改被裁剪图片当前像素的值透明
*currentPixel = RGBAMake(0, 0, 0, 0);
}
}//裁剪
}
// 4. Create a new UIImage
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage]; // 5. Cleanup!
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CGContextRelease(maskContext);
free(inputPixels);
free(maskPixels);
return processedImage;
}
#undef RGBAMake
#undef R
#undef G
#undef B
#undef A
#undef Mask8

0x10 效果

  1. 蒙板图片

  1. 裁剪效果

0x11 注意事项和缺点

  1. 使用时注意要让裁剪图的大小与蒙板的图片大小样同
  2. 图片的大小没有变小,只是修改了像素的颜色和透明度

iOS图片的伪裁剪(改变图片的像素值)的更多相关文章

  1. [转载] IOS 获取网络图片的大小 改变 图片色值 灰度什么的方法集合

    IOS 获取网络图片的大小 改变 图片色值 灰度什么的方法集合

  2. AE IRasterCursor 改变栅格图层像素值

    1 public void ChangePixelValue(double xMax, double xMin, double yMax, double yMin,double[,] PixelCha ...

  3. iOS开发笔记--使用blend改变图片颜色

    最近对Core Animation和Core Graphics的内容东西比较感兴趣,自己之前也在这块相对薄弱,趁此机会也想补习一下这块的内容,所以之后几篇可能都会是对CA和CG学习的记录的文章. 在应 ...

  4. Android--ColorMatrix改变图片颜色

    前言 本篇博客讲解如何通过改变图片像素点RGB的值的方式,在Android中改变图片的颜色.在最后将以一个简单的Demo来作为演示. 本篇博客的主要内容: ColorMatrix 使用ColorMat ...

  5. 使用jQuery动态改变图片显示大小

    当我们要显示后台传过来若干个尺寸不一的图片时,为了保证图片大小的一致性及比例的协调,需要动态改变图片显示尺寸.通过搜索,我们可以从网上找到实现此 功能的jQuery代码如下.这段代码可以使图片的大小保 ...

  6. ios 改变图片大小缩放方法

    http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...

  7. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  8. iOS开发系列--无限循环的图片浏览器

    --UIKit之UIScrollView 概述 UIKit框架中有大量的控件供开发者使用,在iOS开发中不仅可以直接使用这些控件还可以在这些控件的基础上进行扩展打造自己的控件.在这个系列中如果每个控件 ...

  9. java改变图片文件尺寸

    package test.common; import java.awt.Graphics; import java.awt.Image; import java.awt.image.Buffered ...

随机推荐

  1. Longest Consecutive Sequence——Leetcode

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  2. Unity3d 使用DX11的曲面细分

    Unity3d surface Shaderswith DX11 Tessellation Unity3d surface shader 在DX11上的曲面细分 I write this articl ...

  3. 【狼】openGL 光照的学习

    小狼学习原创,欢迎批评指正 http://www.cnblogs.com/zhanlang96/p/3859439.html 先上代码 #include "stdafx.h" #i ...

  4. Ural 1258 镜面对称

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...

  5. zabbix2.2.3 VMware Vsphere exsi监控配置步骤

    zabbix2.2.3 VMware Vsphere exsi监控配置步骤, 1,添加监控主机 2,添加聚集macro;{$PASSWORD} = yoodo.com{$URL} = http://i ...

  6. Google表单

    本博文的主要内容有 .Google表单的介绍 https://www.google.com/intl/zh-CN/forms/about/ 自行去注册Google账号,不多,赘述.

  7. C# 调试

    1.监视窗口

  8. php中strlen和{}的效率对比

    很少有人知道{}用来判断字符串长度 今天试试 发现好像没有strlen快

  9. sqlserver 遇到以零作除数错误的处理 不报错的解决方法

    使用sqlserver 的选项来禁止出现以零除的错误中断,让而让其为null set ansi_warnings offSET ARITHABORT offSET ARITHIGNORE on sel ...

  10. BootStrap-table 客户端分页和服务端分页的区别

    当服务器没有对数据进行分页时,前端页面设计又要求进行分页,要分开来设置. 服务端分页: responseHandler: function(data){ return data.response; } ...