嘿想模糊我的观点,我使用这段代码:
//Get a UIImage from the UIView
NSLog(@"blur capture");
UIGraphicsBeginImageContext(BlurContrainerView.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Blur the UIImage
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 5] forKey: @"inputRadius"]; //change number to increase/decrease blur
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
//create UIImage from filtered image
blurredImage = [[UIImage alloc] initWithCIImage:resultImage];
//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = blurredImage;
NSLog(@"%f,%f",newView.frame.size.width,newView.frame.size.height);
//insert blur UIImageView below transparent view inside the blur image container
[BlurContrainerView insertSubview:newView belowSubview:transparentView];
它使观点模糊,但不是全部。我怎样才能模糊所有的视图?
答案 0 :(得分:19)
问题不在于它不会使所有图像模糊,而是模糊会扩展图像的边界,使图像更大,并且因此不能正确排列。
要使图像保持相同的大小,请执行以下行:
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
您可以抓取CGRect
获取原始图片大小的矩形resultImage
:
// note, adjust rect because blur changed size of image
CGRect rect = [resultImage extent];
rect.origin.x += (rect.size.width - viewImage.size.width ) / 2;
rect.origin.y += (rect.size.height - viewImage.size.height) / 2;
rect.size = viewImage.size;
然后使用CIContext
抓取图片的那一部分:
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:resultImage fromRect:rect];
UIImage *blurredImage = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
或者,对于iOS 7,如果您转到iOS UIImageEffects sample code并下载iOS_UIImageEffects.zip
,则可以获取UIImage+ImageEffects
类别。无论如何,这提供了一些新的方法:
- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
因此,为了模糊和成像并使其变亮(给予“磨砂玻璃”效果),您可以这样做:
UIImage *newImage = [image applyLightEffect];
有趣的是,Apple的代码不使用CIFilter
,而是调用vImageBoxConvolve_ARGB8888的vImage high-performance image processing framework。 WWDC 2013视频Implementing Engaging UI on iOS中说明了这种技术。
答案 1 :(得分:8)
更快的解决方案是完全避免使用CGImageRef并在CIImage惰性级别执行所有转换。
所以,而不是你的不合适:
// cropping rect because blur changed size of image
CIImage *croppedImage = [resultImage imageByCroppingToRect:imageToBlur.extent];
// create UIImage from filtered cropped image
blurredImage = [[UIImage alloc] initWithCIImage:croppedImage];
一个很好的解决方案是写:
// cropping rect because blur changed size of image
let croppedImage = resultImage.cropping(to: imageToBlur.extent)
// create UIImage from filtered cropped image
let blurredImage = UIImage(ciImage: croppedImage)
// cropping rect because blur changed size of image
let croppedImage = resultImage.cropped(to: imageToBlur.extent)
// create UIImage from filtered cropped image
let blurredImage = UIImage(ciImage: croppedImage)
val list = List(50176482, 50176481, 50176485, 50176479, 50176478, 51176477, 51176483, 51176480)
val list1 = list.filter(_.toString.substring(0, 2) == "50")
val list2 = list.filter(_.toString.substring(0, 2) == "51")
println(list1)
println(list2)
答案 2 :(得分:0)
看起来模糊滤镜会给你一个比你开始时更大的图像,这是有道理的,因为边缘处的像素越来越模糊。最简单的解决方案可能是让newView
使用contentMode
UIViewContentModeCenter
,这样就不会试图压缩模糊的图像;你也可以通过在适当大小的新上下文的中心绘制它来裁剪blurredImage
,但你真的不需要。{/ p>