UIImage图片处理(更改颜色,修改透明度,缩小,合并图片)

(2013-10-29 11:34:53)

标签:

分类:

//遍历图片像素,更改图片颜色

void ProviderReleaseData (void *info, constvoid *data, size_t size)

{

free((void*)data);

}

- (UIImage*) p_w_picpathBlackToTransparent:(UIImage*) p_w_picpath

{

// 分配内存

constint p_w_picpathWidth = p_w_picpath.size.width;

constint p_w_picpathHeight = p_w_picpath.size.height;

size_t bytesPerRow = p_w_picpathWidth * 4;

uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * p_w_picpathHeight);

 

// 创建context

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(rgbImageBuf, p_w_picpathWidth, p_w_picpathHeight, 8, bytesPerRow, colorSpace,

kCGBitmapByteOrder32Little |kCGImageAlphaNoneSkipLast);

CGContextDrawImage(context, CGRectMake(0, 0, p_w_picpathWidth, p_w_picpathHeight), p_w_picpath.CGImage);

 

// 遍历像素

int pixelNum = p_w_picpathWidth * p_w_picpathHeight;

uint32_t* pCurPtr = rgbImageBuf;

for (int i = 0; i < pixelNum; i++, pCurPtr++)

{

if ((*pCurPtr & 0xFFFFFF00) == 0xffffff00) //将白色变成透明

{

uint8_t* ptr = (uint8_t*)pCurPtr;

ptr[0] = 0;

}

else

{

// 改成下面的代码,会将图片转成想要的颜色

uint8_t* ptr = (uint8_t*)pCurPtr;

ptr[3] = 0; //0~255

ptr[2] = 0;

ptr[1] = 0;

}

 

}

 

// 将内存转成p_w_picpath

CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * p_w_picpathHeight, ProviderReleaseData);

CGImageRef p_w_picpathRef = CGImageCreate(p_w_picpathWidth, p_w_picpathHeight, 8, 32, bytesPerRow, colorSpace,

kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,

NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

 

UIImage* resultUIImage = [UIImagep_w_picpathWithCGImage:p_w_picpathRef];

 

// 释放

CGImageRelease(p_w_picpathRef);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

// free(rgbImageBuf) 创建dataProvider时已提供释放函数,这里不用free

 

return resultUIImage;

}

//设置图片透明度

- (UIImage *)p_w_picpathByApplyingAlpha:(CGFloat)alpha  p_w_picpath:(UIImage*)p_w_picpath

{

UIGraphicsBeginImageContextWithOptions(p_w_picpath.size,NO,0.0f);

 

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGRect area = CGRectMake(0, 0, p_w_picpath.size.width, p_w_picpath.size.height);

 

CGContextScaleCTM(ctx,1, -1);

CGContextTranslateCTM(ctx,0, -area.size.height);

 

CGContextSetBlendMode(ctx, kCGBlendModeMultiply);

 

CGContextSetAlpha(ctx, alpha);

 

CGContextDrawImage(ctx, area, p_w_picpath.CGImage);

 

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

 

return newImage;

}

//合并2张图片

- (UIImage *)addImage:(UIImage *)p_w_picpath1 toImage:(UIImage *)p_w_picpath2 {

UIGraphicsBeginImageContext(p_w_picpath2.size);

 

// Draw p_w_picpath1

[p_w_picpath1 drawInRect:CGRectMake(0, 0, p_w_picpath1.size.width, p_w_picpath1.size.height)];

 

// Draw p_w_picpath2

[p_w_picpath2 drawInRect:CGRectMake(0, 0, p_w_picpath2.size.width, p_w_picpath2.size.height)];

 

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

//将UIImage缩放到指定大小尺寸:

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{

// 创建一个bitmap的context

// 并把它设置成为当前正在使用的context

UIGraphicsBeginImageContext(size);

// 绘制改变大小的图片

[img drawInRect:CGRectMake(0,0, size.width, size.height)];

// 从当前context中创建一个改变大小后的图片

UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// 使当前的context出堆栈

UIGraphicsEndImageContext();

// 返回新的改变大小后的图片

return scaledImage;

}

//根据图片的大小等比例压缩返回图片

-(UIImage *)fitSmallImage:(UIImage *)p_w_picpath size:(CGSize)size

{

if (nil == p_w_picpath)

{

returnnil;

}

if (p_w_picpath.size.width<<span class="s5">200 && p_w_picpath.size.height<<span class="s5">200)

{

return p_w_picpath;

}

UIGraphicsBeginImageContext(size);

CGRect rect = CGRectMake(0, 0, size.width, size.height);

[p_w_picpath drawInRect:rect];

UIImage *newing = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newing;

}