//使用CGImage获取并修改图片像素

#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 *)processUsingPixels:(UIImage*)inputImage {
// 1. Get the raw pixels of the image
//定义最高32位整形指针 *inputPixels
UInt32 * inputPixels; //转换图片为CGImageRef,获取参数:长宽高,每个像素的字节数(4),每个R的比特数
CGImageRef inputCGImage = [inputImage CGImage];
NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
NSUInteger inputHeight = CGImageGetHeight(inputCGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSUInteger bytesPerPixel = ;
NSUInteger bitsPerComponent = ; //每行字节数
NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth; //开辟内存区域,指向首像素地址
inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32)); //根据指针,前面的参数,创建像素层
CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
bitsPerComponent, inputBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
//根据目前像素在界面绘制图像
CGContextDrawImage(context, CGRectMake(, , inputWidth, inputHeight), inputCGImage); //接来下就是重点了!!!像素处理--------------------------------------------------------
for (NSUInteger j = ; j < inputHeight; j++) {
for (NSUInteger i = ; i < inputWidth; i++) {
UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;
UInt32 color = *currentPixel;
UInt32 br,thisR,thisG,thisB,thisA;
//这里直接移位获得RBGA的值,以及输出写的非常好!
thisR=R(color);
thisG=G(color);
thisB=B(color);
thisA=A(color);
//NSLog(@"%d,%d,%d,%d",thisR,thisG,thisB,thisA);
br=B(color)-R(color);
*currentPixel = RGBAMake(br, br, br, A(color));
}
}
//创建新图
// 4. Create a new UIImage
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];
//释放
// 5. Cleanup!
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
free(inputPixels); return processedImage;
}
//=======图像缩放====================================================================
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(, , image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); NSLog(@"准备图像裁剪》》》》缩放后图片大小:width %f height %f ",scaledImage.size.width,scaledImage.size.height); UIGraphicsEndImageContext();
return scaledImage;
}

[iOS 图像处理相关]的更多相关文章

  1. iOS网络相关知识总结

    iOS网络相关知识总结 1.关于请求NSURLRequest? 我们经常讲的GET/POST/PUT等请求是指我们要向服务器发出的NSMutableURLRequest的类型; 我们可以设置Reque ...

  2. iOS网络相关零散知识总结

    iOS网络相关零散知识总结 1. URL和HTTP知识 (1) URL的全称是Uniform Resource Locator(统一资源定位符). URL的基本格式 = 协议://主机地址/路径   ...

  3. 详解OS X和iOS图像处理框架Core Image

    转自:http://www.csdn.net/article/2015-02-13/2823961-core-image 摘要:本 文结合实例详解了OS X和iOS图像处理框架Core Image的使 ...

  4. iOS - 直播相关文章

    直播相关文章 直播RTMP可用于测试的服务器地址 FFmpeg avdumpformat输出的tbn.tbc.tbr.PAR.DAR的含义 FFmpeg 3.0 计算视频时长 HLS Streamin ...

  5. ios 缓存相关信息收集

    链接:http://www.cnblogs.com/pengyingh/category/353093.html 使用NSURLCache让本地数据来代替远程UIWebView请求 摘要: 原文作者: ...

  6. iOS开发-相关文档

    关于调试,查看Xcode Overview文档相关部分:http://developer.apple.com/library/ios/documentation/ToolsLanguages/Conc ...

  7. iOS开发经验相关知识

    一. iPhone Size 手机型号 屏幕尺寸 iPhone 4 4s 320 * 480 iPhone 5 5s 320 * 568 iPhone 6 6s 375 * 667 iphone 6 ...

  8. iOS app 支持HTTPS iOS开发者相关

    2016年12月21日更新开发者中心链接https://developer.apple.com/news/?id=12212016b该链接是苹果昨天刚在官网给的正式回复 如下: App Transpo ...

  9. iOS 地图相关

    参考博文:https://blog.csdn.net/zhengang007/article/details/52858198?utm_source=blogxgwz7 1.坐标系 目前常见的坐标系有 ...

随机推荐

  1. struts2使用Convention Plugin在weblogic上以war包部署时,找不到Action的解决办法

    环境: struts 2.3.16.3 + Convention Plugin 2.3.16.3 实现零配置 现象:以文件夹方式部署在weblogic(10.3.3)上时一切正常,换成war包部署,运 ...

  2. "org.jboss.netty.internal.LoggerConfigurator".DESCRIBED is already registered 的解决办法

    今天在jboss 6.2 EAP上部署一个项目时,报以下错误: org.jboss.msc.service.DuplicateServiceException: Service jboss.pojo. ...

  3. Pechkin:html -> pdf 利器

    Pechkin 是GitHub上的一个开源项目,可方便将html转化成pdf文档,使用也很方便,下面是winform项目中的示例代码: using System; using System.Diagn ...

  4. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  5. 对atime、mtime和ctime的研究

    前期准备 在实验之前我们在讨论为何会出现两种修改时间,为此我们推测因为修改的不是文件的同一数据,或者说同一地方,那么我们就要先搞清楚文件的结构. linux文件系统是Linux系统的心脏部分,提供了层 ...

  6. favicon.ico文件简介

    本地调试时,控制台经常会打印如下的错误(对 favicon.ico 的请求在 chrome 调试面板下不可见,可在抓包工具,比如 Fiddler 中看到): favicon.ico 是啥?看下面这张图 ...

  7. WPF下制作的简单瀑布流效果

    最近又在搞点小东西,美化界面的时候发现瀑布流效果比较不错.顺便就搬到了WPF,下面是界面 我对WEB前端不熟,JS和CSS怎么实现的,我没去研究过,这里就说下WPF的实现思路,相当简单. 1.最重要的 ...

  8. 【Android 开发】: Android 消息处理机制之一: Handler 与 Message

    最近几讲内容,我们学习了Android中关于多线程的一些知识,上一讲我们讲解了异步任务 AsyncTask 的操作,Android中还提供了其他的线程操作,如Handler Message Messa ...

  9. android开发------编写用户界面之线性布局

    一个好的应用程序离不开人性化的用户界面.在学习其他东西之前.理应先学习编写程序的布局(外观) 今天,我们就来学习android的UI布局----LinearLayout. LinearLayout,即 ...

  10. canvas三角函数应用

    这个是圆圈旋转的简单案例 var canvas=document.getElementById("canvas"); var cxt=canvas.getContext(" ...