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;
//
学习在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;
//
相关文章推荐
- iOS开发之保存照片到自己创建的相簿
- iOS开发之保存照片到系统相册(Photo Album)
- iOS7之后如何正确隐藏状态栏
- iOS学习----------详解FFMPEG API
- iOS开发-retain/assign/strong/weak/copy/mutablecopy/autorelease区别
- Nagios远程监控windows方案补充说明
- IOS开发中的几种设计模式介绍
- iOS基本设计模式
- iOS实现View镂空效果
- ios 短音效的使用
- IOS第三天_面向对象1
- ios 拖线没有反应
- 收集Github上的iOS控件和开发资料
- iOS8定位问题解决方案
- IOS测试:XCTest小试牛刀
- ios类别和继承的区别
- iOS 得到圆形头像的方法
- ios-第六天笔记
- IOS-第七天笔记
- iOS: 枚举类型 enum,NS_ENUM,NS_OPTIONS