iOS 模糊图像的解决方案
来源:互联网 发布:半全局匹配算法 编辑:程序博客网 时间:2024/05/19 08:42
#import <QuartzCore/QuartzCore.h>- (UIImage*) blur:(UIImage*)theImage{ // ***********If you need re-orienting (e.g. trying to blur a photo taken from the device camera front facing camera in portrait mode) // theImage = [self reOrientIfNeeded:theImage]; // create our blurred image CIContext *context = [CIContext contextWithOptions:nil]; CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage]; // setting up Gaussian Blur (we could use one of many filters offered by Core Image) CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; [filter setValue:inputImage forKey:kCIInputImageKey]; [filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"]; CIImage *result = [filter valueForKey:kCIOutputImageKey]; // CIGaussianBlur has a tendency to shrink the image a little, // this ensures it matches up exactly to the bounds of our original image CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; UIImage *returnImage = [UIImage imageWithCGImage:cgImage];//create a UIImage for this function to "return" so that ARC can manage the memory of the blur... ARC can't manage CGImageRefs so we need to release it before this function "returns" and ends. CGImageRelease(cgImage);//release CGImageRef because ARC doesn't manage this on its own. return returnImage; // *************** if you need scaling // return [[self class] scaleIfNeeded:cgImage];}+(UIImage*) scaleIfNeeded:(CGImageRef)cgimg { bool isRetina = [[[UIDevice currentDevice] systemVersion] intValue] >= 4 && [[UIScreen mainScreen] scale] == 2.0; if (isRetina) { return [UIImage imageWithCGImage:cgimg scale:2.0 orientation:UIImageOrientationUp]; } else { return [UIImage imageWithCGImage:cgimg]; }}- (UIImage*) reOrientIfNeeded:(UIImage*)theImage{ if (theImage.imageOrientation != UIImageOrientationUp) { CGAffineTransform reOrient = CGAffineTransformIdentity; switch (theImage.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: reOrient = CGAffineTransformTranslate(reOrient, theImage.size.width, theImage.size.height); reOrient = CGAffineTransformRotate(reOrient, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: reOrient = CGAffineTransformTranslate(reOrient, theImage.size.width, 0); reOrient = CGAffineTransformRotate(reOrient, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: reOrient = CGAffineTransformTranslate(reOrient, 0, theImage.size.height); reOrient = CGAffineTransformRotate(reOrient, -M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } switch (theImage.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: reOrient = CGAffineTransformTranslate(reOrient, theImage.size.width, 0); reOrient = CGAffineTransformScale(reOrient, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: reOrient = CGAffineTransformTranslate(reOrient, theImage.size.height, 0); reOrient = CGAffineTransformScale(reOrient, -1, 1); break; case UIImageOrientationUp: case UIImageOrientationDown: case UIImageOrientationLeft: case UIImageOrientationRight: break; } CGContextRef myContext = CGBitmapContextCreate(NULL, theImage.size.width, theImage.size.height, CGImageGetBitsPerComponent(theImage.CGImage), 0, CGImageGetColorSpace(theImage.CGImage), CGImageGetBitmapInfo(theImage.CGImage)); CGContextConcatCTM(myContext, reOrient); switch (theImage.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: CGContextDrawImage(myContext, CGRectMake(0,0,theImage.size.height,theImage.size.width), theImage.CGImage); break; default: CGContextDrawImage(myContext, CGRectMake(0,0,theImage.size.width,theImage.size.height), theImage.CGImage); break; } CGImageRef CGImg = CGBitmapContextCreateImage(myContext); theImage = [UIImage imageWithCGImage:CGImg]; CGImageRelease(CGImg); CGContextRelease(myContext); } return theImage;}转载自:http://stackoverflow.com/a/17041983/3458781
0 0
- iOS 模糊图像的解决方案
- 图像模糊的种类
- 图像的去模糊
- iOS 图像处理 - 模糊图像
- IOS图像拉伸解决方案
- IOS图像拉伸解决方案
- 关于canvas在retina屏下绘制文字或图像模糊的解决方案
- 关于canvas在retina屏下绘制文字或图像模糊的解决方案
- 边界模糊的图像分割
- 图像的模糊处理原理
- opencv3的图像模糊处理
- 【图像处理】【去模糊】图像去模糊的原理
- iOS开发_修改系统cell的图像会变模糊的处理
- kidd风的IOS日志之学习用MetalPerformanceShaders进行图像处理(模糊效果)
- 模糊图像退化与去模糊的数学模型
- WPF中图像模糊的处理方法
- OpenCV实现运动模糊图像的模拟
- 图像去模糊的一些网站
- 时序分析:串匹配—Brute-Force算法
- c++中strlen和sizeof区别
- IEEE 754 浮点数的四种舍入方式
- Linux进程间通信——使用数据报套接字
- 牛腩新闻发布系统之HTTP错误403.14
- iOS 模糊图像的解决方案
- 在javascript文件中使用jstl标签
- POJ2449 Remmarguts' Date 【k短路】
- Struts+JSON+Struts+JQuery小列子
- 计算机管理无法打开解决办法
- C++ int转string(stringstream可转更多类型)
- hd 2022 海选女主角
- Objective-C Prperty
- [LeetCode] Evaluate Reverse Polish Notation