您的位置:首页 > 移动开发 > IOS开发

iOS学习----------图片的简单处理(1)

2016-02-17 23:33 549 查看
效果图



学习在iOS中处理图像和创建酷炫的效果!

第一种方法:获取每个像素将两张图片合并

- (UIImage *)processUsingPixels:(UIImage*)inputImage
{

// 1. Get the raw pixels of the image
UInt32 * inputPixels;//定义一个整型的指针

CGImageRef inputCGImage = [inputImageCGImage];
NSUInteger inputWidth =CGImageGetWidth(inputCGImage);
NSUInteger inputHeight =CGImageGetHeight(inputCGImage);

//创建一个RGB模式的颜色空间CGColorSpace和一个容器CGBitmapContext,将像素指针参数传递到容器中缓存进行存储

/*

参数bytesPerPixel(每像素大小)和bitsPerComponent(每个颜色通道大小)

*/
NSUInteger bytesPerPixel =4;
NSUInteger bitsPerComponent =8;

//输入的每行
NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;// 4*3000

//calloc在动态分配完内存后,自动初始化该内存空间为零

//初始化一个二维整型数组 inputHeight行(row)
inputWidth列(col) 数组中每个对象的为UInt32的大小
inputPixels = (UInt32 *)calloc(inputHeight * inputWidth,sizeof(UInt32));

//创建一个RGB模式的颜色空间CGColorSpace和一个容器CGBitmapContext,将像素指针参数传递到容器中缓存进行存储

CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

/*

CGContextRef __nullable CGBitmapContextCreate(void * __nullable data,

size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow,

CGColorSpaceRef __nullable space, uint32_t bitmapInfo)

*/
CGContextRef context =CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
bitsPerComponent, inputBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast |kCGBitmapByteOrder32Big);

//将设定好的图片绘制在上下文(context)中
CGContextDrawImage(context,CGRectMake(0,0,
inputWidth, inputHeight), inputCGImage);

// 2. Blend the ghost onto the image
UIImage * ghostImage = [UIImageimageNamed:@"ghost"];
CGImageRef ghostCGImage = [ghostImageCGImage];

// 2.1 Calculate the size & position of the ghost

//宽高比
CGFloat ghostImageAspectRatio = ghostImage.size.width
/ ghostImage.size.height;

//宽度等于低部图片的1/4
NSInteger targetGhostWidth = inputWidth *0.25;

//
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: