博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIImage图片处理
阅读量:7212 次
发布时间:2019-06-29

本文共 2829 字,大约阅读时间需要 9 分钟。

#pragma mark -#pragma mark - 缩放处理+ (UIImage *)scaleImage:(UIImage *)image withScale:(float)scale{    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale));        [image drawInRect:CGRectMake(0, 0, image.size.width * scale, image.size.height * scale)];    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return scaledImage;}+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size{    UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));        [image drawInRect:CGRectMake(0, 0, size.width, size.height)];    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return scaledImage;}#pragma mark -#pragma mark - 马赛克处理static CGContextRef CreateRGBABitmapContext (CGImageRef inImage){    CGContextRef context = NULL;    CGColorSpaceRef colorSpace;    void *bitmapData; //内存空间的指针,该内存空间的大小等于图像使用RGB通道所占用的字节数。    int bitmapByteCount;    int bitmapBytesPerRow;        size_t pixelsWide = CGImageGetWidth(inImage); //获取横向的像素点的个数    size_t pixelsHigh = CGImageGetHeight(inImage);        bitmapBytesPerRow    = (pixelsWide * 4); //每一行的像素点占用的字节数,每个像素点的ARGB四个通道各占8个bit(0-255)的空间    bitmapByteCount    = (bitmapBytesPerRow * pixelsHigh); //计算整张图占用的字节数        colorSpace = CGColorSpaceCreateDeviceRGB();//创建依赖于设备的RGB通道    //分配足够容纳图片字节数的内存空间    bitmapData = malloc( bitmapByteCount );    //创建CoreGraphic的图形上下文,该上下文描述了bitmaData指向的内存空间需要绘制的图像的一些绘制参数    context = CGBitmapContextCreate (bitmapData,                                     pixelsWide,                                     pixelsHigh,                                     8,                                     bitmapBytesPerRow,                                     colorSpace,                                     kCGImageAlphaPremultipliedLast);    //Core Foundation中通过含有Create、Alloc的方法名字创建的指针,需要使用CFRelease()函数释放    CGColorSpaceRelease( colorSpace );    return context;}static unsigned char *RequestImagePixelData(UIImage *inImage){    CGImageRef img = [inImage CGImage];    CGSize size = [inImage size];    //使用上面的函数创建上下文    CGContextRef cgctx = CreateRGBABitmapContext(img);        CGRect rect = {
{0,0},{size.width, size.height}}; //将目标图像绘制到指定的上下文,实际为上下文内的bitmapData。 CGContextDrawImage(cgctx, rect, img); unsigned char *data = CGBitmapContextGetData (cgctx); //释放上面的函数创建的上下文 CGContextRelease(cgctx); return data;}+ (UIImage *)mosaicImage:(UIImage *)image withLevel:(int)level{ unsigned char *imgPixel = RequestImagePixelData(image); CGImageRef inImageRef = [image CGImage]; GLuint width = CGImageGetWidth(inImageRef); GLuint height = CGImageGetHeight(inImageRef); unsigned char prev[4] = {0}; int bytewidth = width*4; int i,j; int val = level; for(i=0;i

  

转载地址:http://nguym.baihongyu.com/

你可能感兴趣的文章
bootstrap的流式布局
查看>>
如何通过线程池异步调用
查看>>
Squid配置详解
查看>>
070104_微积分:随机变量及其分布(二项分布,均匀分布,正态分布)
查看>>
LeetCode – Refresh – Binary Tree Zigzag Level Order Traversal
查看>>
python操作三大主流数据库(13)python操作redis之新闻项目实战①新闻数据的导入
查看>>
2013夏,iDempiere来了 - v1.0c Installers (Devina LTS Release) 2013-06-27
查看>>
每天一个linux命令(22):find 命令的参数详解
查看>>
然后是几点(15)
查看>>
15.节点属性
查看>>
ISO-8859-1编码
查看>>
PHP 代码评审的 10 个提示
查看>>
你知道吗?Web的26项基本概念和技术
查看>>
方案优化:网站实现扫描二维码关注微信公众号,自动登陆网站并获取其信息...
查看>>
Leetcode | Balanced Binary Tree
查看>>
sqlServer对内存的管理
查看>>
挑战密室
查看>>
利用Solr服务建立的站内搜索雏形---solr1
查看>>
5、jmeter-逻辑控制器介绍与使用
查看>>
如何遍历List对象
查看>>