iOS学习----------图片的简单处理(2)
来源:互联网 发布:mac qq五笔拼音输入法 编辑:程序博客网 时间:2024/06/10 03:05
接第一部分内容
//图片的大小
CGSize ghostSize = CGSizeMake(targetGhostWidth, targetGhostWidth / ghostImageAspectRatio);
//图片的位置
CGPoint ghostOrigin = CGPointMake(inputWidth * 0.5, inputHeight *0.2);
// 2.2 Scale & Get pixels of the ghost
NSUInteger ghostBytesPerRow = bytesPerPixel * ghostSize.width;
UInt32 * ghostPixels = (UInt32 *)calloc(ghostSize.width * ghostSize.height,sizeof(UInt32));
CGContextRef ghostContext = CGBitmapContextCreate(ghostPixels, ghostSize.width, ghostSize.height,
bitsPerComponent, ghostBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(ghostContext, CGRectMake(0, 0, ghostSize.width, ghostSize.height),ghostCGImage);
// 2.3 Blend each pixel
NSUInteger offsetPixelCountForInput = ghostOrigin.y * inputWidth + ghostOrigin.x;
for (NSUInteger j =0; j < ghostSize.height; j++) {
for (NSUInteger i =0; i < ghostSize.width; i++) {
UInt32 * inputPixel = inputPixels + j * inputWidth + i + offsetPixelCountForInput;
UInt32 inputColor = *inputPixel;
UInt32 * ghostPixel = ghostPixels + j * (int)ghostSize.width + i;
UInt32 ghostColor = *ghostPixel;
// Blend the ghost with 50% alpha
CGFloat ghostAlpha = 0.5f * (A(ghostColor) / 255.0);
UInt32 newR = R(inputColor) * (1 - ghostAlpha) +R(ghostColor) * ghostAlpha;
UInt32 newG = G(inputColor) * (1 - ghostAlpha) +G(ghostColor) * ghostAlpha;
UInt32 newB = B(inputColor) * (1 - ghostAlpha) +B(ghostColor) * ghostAlpha;
//Clamp, not really useful here :p
newR =MAX(0,MIN(255, newR));
newG =MAX(0,MIN(255, newG));
newB =MAX(0,MIN(255, newB));
*inputPixel =RGBAMake(newR, newG, newB, A(inputColor));
}
}
// 3. Convert the image to Black & White
for (NSUInteger j =0; j < inputHeight; j++) {
for (NSUInteger i =0; i < inputWidth; i++) {
UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;
UInt32 color = *currentPixel;
// Average of RGB = greyscale
UInt32 averageColor = (R(color) +G(color) + B(color)) /3.0;
*currentPixel =RGBAMake(averageColor, averageColor, averageColor,A(color));
}
}
// 4. Create a new UIImage
//将context中画好的图片转化为CGImage
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImageimageWithCGImage:newCGImage];
// 5. Cleanup!
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CGContextRelease(ghostContext);
free(inputPixels);
free(ghostPixels);
return processedImage;
}
- ios - 图片处理
- iOS学习----------图片的简单处理(2)
- iOS学习----------图片的简单处理(1)
- IOS应用开发-图片处理
- IOS学习 -两个图片切换显示
- iOS客户端学习-文本框带收索图片
- iOS开发图像处理--绘制图片上下颠倒
- iOS开发图像处理--绘制图片上下颠倒 .
- iOS开发图像处理--绘制图片上下颠倒 .
- iOS开发图像处理--绘制图片上下颠倒
- iOS开发图像处理--绘制图片上下颠倒
- iOS开发图像处理--绘制图片上下颠倒
- iOS开发图像处理--绘制图片上下颠倒
- iOS开发图像处理--绘制图片上下颠倒
- IOS应用开发-图片处理(拉伸图片 创建缩略图 解决图片旋转的问题 图片编码及上传 将图片写入磁盘)
- IOS应用开发-图片处理(拉伸图片 创建缩略图 解决图片旋转的问题 图片编码及上传 将图片写入磁盘)
- ios客户端学习-ios及android改变图片颜色的方法
- iOS--异常处理
- Pig 初识
- 链队列的基本操作
- Bootstrap多级导航栏(级联导航)的实现代码
- 消除“Permission is only granted to system apps”错误
- HDU5605——数学题(三角函数的应用)
- iOS学习----------图片的简单处理(2)
- socket访问百度
- 关于Activity生命周期
- 华为笔试1 去除重复字符并排序的字符串 c++ string 实现
- 六种常用的设计模式java实现(三)责任链模式
- 1084. Broken Keyboard (20)
- 【数据结构与算法】数据结构+算法=程序
- 安卓开发:将数据返回给上一个活动
- hdu 1535 Invitation Cards(邻接表spfa)