下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
第一种使用Core Image进行模糊- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGfloat)blur { CIImage *inputimage = [CIImage imageWithCGImage:image.CGImage]; CIFilter *filter = [CIFilter filterWithname:@"CIGaussianBlur" keysAndValues:kCIInputimageKey,inputimage,@"inputRadius",@(blur),nil]; CIImage *outputimage = filter.outputimage; CGImageRef outimage = [self.context createCGImage:outputimage fromrect:[outputimage extent]]; return [UIImage imageWithCGImage:outimage]; }
第二种使用vImage API进行模糊
- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGfloat)blur { if (blur < 0.f || blur > 1.f) { blur = 0.5f; } int BoxSize = (int)(blur * 100); BoxSize = BoxSize - (BoxSize % 2) + 1; CGImageRef img = image.CGImage; vImage_Buffer inBuffer,outBuffer; vImage_Error error; voID *pixelBuffer; CGDataProvIDerRef inProvIDer = CGImageGetDataProvIDer(img); CFDataRef inBitmapData = CGDataProvIDercopyData(inProvIDer); inBuffer.wIDth = CGImageGetWIDth(img); inBuffer.height = CGImageGetHeight(img); inBuffer.rowBytes = CGImageGetBytesPerRow(img); inBuffer.data = (voID*)CFDataGetBytePtr(inBitmapData); pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img)); if(pixelBuffer == NulL) NSLog(@"No pixelbuffer"); outBuffer.data = pixelBuffer; outBuffer.wIDth = CGImageGetWIDth(img); outBuffer.height = CGImageGetHeight(img); outBuffer.rowBytes = CGImageGetBytesPerRow(img); error = vImageBoxConvolve_ARGB8888(&inBuffer,&outBuffer,NulL,BoxSize,kvImageEdgeExtend); if (error) { NSLog(@"error from convolution %ld",error); } CGcolorSpaceRef colorSpace = CGcolorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate( outBuffer.data,outBuffer.wIDth,outBuffer.height,8,outBuffer.rowBytes,colorSpace,kCGImageAlphaNoneskipLast); CGImageRef imageRef = CGBitmapContextCreateImage (ctx); UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; //clean up CGContextRelease(ctx); CGcolorSpaceRelease(colorSpace); free(pixelBuffer); CFRelease(inBitmapData); CGcolorSpaceRelease(colorSpace); CGImageRelease(imageRef); return returnImage; }
第三种方法是网上找到的(毛玻璃效果)
// 内部方法,核心代码,封装了毛玻璃效果 参数:半径,颜色,色彩饱和度- (UIImage *)imageBlureDWithRadius:(CGfloat)blurRadius tintcolor:(UIcolor *)tintcolor saturationDeltaFactor:(CGfloat)saturationDeltaFactor maskImage:(UIImage *)maskImage { CGRect imageRect = { CGPointZero,self.size }; UIImage *effectimage = self; BOol hasBlur = blurRadius > __FLT_EPSILON__; BOol hasSaturationChange = fabs(saturationDeltaFactor - 1.) > __FLT_EPSILON__; if (hasBlur || hasSaturationChange) { UIGraphicsBeginImageContextWithOptions(self.size,NO,[[UIScreen mainScreen] scale]); CGContextRef effectInContext = UIGraphicsGetCurrentContext(); CGContextScaleCTM(effectInContext,1.0,-1.0); CGContextTranslateCTM(effectInContext,-self.size.height); CGContextDrawImage(effectInContext,imageRect,self.CGImage); vImage_Buffer effectInBuffer; effectInBuffer.data = CGBitmapContextGetData(effectInContext); effectInBuffer.wIDth = CGBitmapContextGetWIDth(effectInContext); effectInBuffer.height = CGBitmapContextGetHeight(effectInContext); effectInBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectInContext); UIGraphicsBeginImageContextWithOptions(self.size,[[UIScreen mainScreen] scale]); CGContextRef effectOutContext = UIGraphicsGetCurrentContext(); vImage_Buffer effectOutBuffer; effectOutBuffer.data = CGBitmapContextGetData(effectOutContext); effectOutBuffer.wIDth = CGBitmapContextGetWIDth(effectOutContext); effectOutBuffer.height = CGBitmapContextGetHeight(effectOutContext); effectOutBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectOutContext); if (hasBlur) { CGfloat inputRadius = blurRadius * [[UIScreen mainScreen] scale]; NSUInteger radius = floor(inputRadius * 3. * sqrt(2 * M_PI) / 4 + 0.5); if (radius % 2 != 1) { radius += 1; // force radius to be odd so that the three Box-blur methodology works. } vImageBoxConvolve_ARGB8888(&effectInBuffer,&effectOutBuffer,(short)radius,kvImageEdgeExtend); vImageBoxConvolve_ARGB8888(&effectOutBuffer,&effectInBuffer,kvImageEdgeExtend); vImageBoxConvolve_ARGB8888(&effectInBuffer,kvImageEdgeExtend); } BOol effectimageBuffersAreSwapped = NO; if (hasSaturationChange) { CGfloat s = saturationDeltaFactor; CGfloat floatingPointSaturationMatrix[] = { 0.0722 + 0.9278 * s,0.0722 - 0.0722 * s,0.7152 - 0.7152 * s,0.7152 + 0.2848 * s,0.2126 - 0.2126 * s,0.2126 + 0.7873 * s,1,}; const int32_t divisor = 256; NSUInteger matrixSize = sizeof(floatingPointSaturationMatrix)/sizeof(floatingPointSaturationMatrix[0]); int16_t saturationMatrix[matrixSize]; for (NSUInteger i = 0; i < matrixSize; ++i) { saturationMatrix[i] = (int16_t)roundf(floatingPointSaturationMatrix[i] * divisor); } if (hasBlur) { vImageMatrixMultiply_ARGB8888(&effectOutBuffer,saturationMatrix,divisor,kvImageNoFlags); effectimageBuffersAreSwapped = YES; } else { vImageMatrixMultiply_ARGB8888(&effectInBuffer,kvImageNoFlags); } } if (!effectimageBuffersAreSwapped) effectimage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); if (effectimageBuffersAreSwapped) effectimage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); } // 开启上下文 用于输出图像 UIGraphicsBeginImageContextWithOptions(self.size,[[UIScreen mainScreen] scale]); CGContextRef outputContext = UIGraphicsGetCurrentContext(); CGContextScaleCTM(outputContext,-1.0); CGContextTranslateCTM(outputContext,-self.size.height); // 开始画底图 CGContextDrawImage(outputContext,self.CGImage); // 开始画模糊效果 if (hasBlur) { CGContextSaveGState(outputContext); if (maskImage) { CGContextClipToMask(outputContext,maskImage.CGImage); } CGContextDrawImage(outputContext,effectimage.CGImage); CGContextRestoreGState(outputContext); } // 添加颜色渲染 if (tintcolor) { CGContextSaveGState(outputContext); CGContextSetFillcolorWithcolor(outputContext,tintcolor.CGcolor); CGContextFillRect(outputContext,imageRect); CGContextRestoreGState(outputContext); } // 输出成品,并关闭上下文 UIImage *outputimage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); return outputimage;}
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS实现毛玻璃效果,图片模糊效果的三种方法全部内容,希望文章能够帮你解决iOS实现毛玻璃效果,图片模糊效果的三种方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)