一猫态、首先要有一個(gè)操作 ARGB 各個(gè)分量:A佣蓉、R、G亲雪、B 的工具勇凭,在繪制圖片的過程中行像素點(diǎn)的操作
#ifndef Color_h
#define Color_h
#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)
#endif /* Color_h */
二、新建圖片美白的工具類:ImageProcessUtils义辕,并對外提供接口虾标。
其.h 文件中的代碼如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface ImageProcessUtils : NSObject
/**
@param imageSrc 需要被修飾的圖片
@return 新的一張圖片
*/
+ (UIImage *)imageFromEmbellished:(UIImage *)imageSrc;
@end
其.m 文件中的代碼如下:
#import "ImageProcessUtils.h"
#import "Color.h"
@implementation ImageProcessUtils
+ (UIImage *)imageFromEmbellished:(UIImage *)imageSrc
{
// 一、獲取原始的圖片大小有如下兩種方法
/* 第1種方法
CGFloat width = imageSrc.size.width;
CGFloat height = imageSrc.size.height;
*/
// 第2種方法
CGImageRef imageRef = [imageSrc CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
// 二终息、開辟一塊內(nèi)存空間夺巩,用于創(chuàng)建顏色空間
/*
照片有兩種贞让,黑白照和彩照周崭,美白只能對彩照進(jìn)行美白
CGColorSpaceRef graySpaceRef = CGColorSpaceCreateDeviceGray();//灰色空間;
*/
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();// 彩色空間喳张;
// 三续镇、創(chuàng)建圖片上下文,解析圖片信息
/*
參數(shù)1:數(shù)據(jù)源
<1>創(chuàng)建指針:inputPixels 其作用是指向圖片的內(nèi)存區(qū)域销部,方便我們通過指針去操作像素點(diǎn)(內(nèi)存)摸航。圖片其實(shí)是線素?cái)?shù)組,inputPixels這個(gè)指針指向數(shù)組的第一個(gè)元素舅桩,即第一個(gè)像素點(diǎn)(數(shù)組的首地址)
<2>為什么是32位酱虎?
圖片是由一個(gè)個(gè)的像素點(diǎn)構(gòu)成的
像素點(diǎn)由RGB或ARGB或R或G或B或RG等組合構(gòu)成。當(dāng)像素點(diǎn)占內(nèi)存最大為此時(shí)的組合為:ARGB
在圖像學(xué)中 A 代表透明度擂涛,R 代表紅色读串,G 代表綠色,B 代表藍(lán)色
A、R恢暖、G排监、B、四個(gè)分量中的每個(gè)分量所占用的內(nèi)存8位杰捂、在計(jì)算機(jī)中舆床,每8位等于1字節(jié),最大為4個(gè)字節(jié)嫁佳。也就是32位
UInt32表示無符號(hào)(無正負(fù)) 0-255
Int32 有符號(hào)(有正負(fù)) -127-128
參數(shù)2:圖片的寬
參數(shù)3:圖片的高
參數(shù)4:每個(gè)像素點(diǎn)每個(gè)分量的大小----8
參數(shù)5:每一行占用的內(nèi)存大邪ざ印(每一個(gè)像素點(diǎn)的內(nèi)存有4個(gè)字節(jié)*width)
參數(shù)6:顏色空間
參數(shù)7:布局?jǐn)[放(是否需要透明度)
*/
// 創(chuàng)建指針
UInt32 * inputPixels = (UInt32 *)calloc(width * height, sizeof(UInt32));
CGContextRef context = CGBitmapContextCreate(inputPixels, width, height, 8, 4 * width, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);// kCGBitmapByteOrder32Big 計(jì)算機(jī)的原理
// 四、根據(jù)圖片的上下文進(jìn)行繪制
/*
參數(shù)1:圖片上下文
參數(shù)2:繪制區(qū)域
參數(shù)3:圖片指針
*/
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
// 五蒿往、圖片的美白 操作像素點(diǎn)瞒瘸,及操作 ARGB 分量的值
/*
修改分量的值--->增大 (修改像素點(diǎn)、操作內(nèi)存)
循環(huán)遍歷像素點(diǎn)(從左到右 自上而下)外層循環(huán)遍歷行 內(nèi)層循環(huán)遍歷列
*/
int light = 50; // 定義圖片的亮度
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// 首先獲取線素?cái)?shù)組的首地址(指針位移)
UInt32 * currentPixels = inputPixels + (i * width) + j;
// 取出像素點(diǎn)的值(&取地址熄浓,*取值)
UInt32 color = *currentPixels;
// 操作 ARGB 的值 ARGB在內(nèi)存中的排版時(shí)有順序的 通過位運(yùn)算獲取到指定的顏色
// 定義四個(gè)變量還獲取 RGBA 的值
UInt32 thisR, thisG, thisB, thisA;
thisR = R(color);
thisR = thisR + light;
// 判斷是否超出了255情臭;
thisR = thisR > 255? 255 : thisR;
thisG = G(color);
thisG = thisG + light;
thisG = thisG > 255? 255 : thisG;
thisB = B(color);
thisB = thisB + light;
thisB = thisB > 255? 255 : thisB;
// 獲取透明度
thisA = A(color);
// 修改線素的 ARGB 的值
*currentPixels = RGBAMake(thisR, thisG, thisB, thisA);
}
}
// 繪制圖片
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage * newImage = [UIImage imageWithCGImage:newImageRef];
// 釋放內(nèi)存 C語言里面有Creat\new\copy 就需要 釋放 ARC 不負(fù)責(zé)!!
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(context);
CGImageRelease(newImageRef);
free(inputPixels);
return newImage;
}
@end
三、控制器中的演示代碼如下:
#import "ViewController.h"
#import "ImageProcessUtils.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *testImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)backImage:(UIButton *)sender {
_testImageView.image = [UIImage imageNamed:@"timg"];
}
- (IBAction)meibaiImage:(UIButton *)sender {
_testImageView.image = [ImageProcessUtils imageFromEmbellished:_testImageView.image];
}
最后:
1赌蔑、大家不喜勿噴俯在,喜歡的可以 star
2、demo見 gitHub