开发者社区> 问答> 正文

iphone中模糊图片的指定区域

在iPhone中怎么样模糊图片的指定区域?我在网上找到的都是模糊整个图片的方法,有没有只模糊指定区域(圆形,方形)的方法?

展开
收起
爵霸 2016-03-26 10:04:20 2292 0
1 条回答
写回答
取消 提交回答
  • 把下面的Pangesture添加到view或者imageview中:

    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imageTaped:)];
    [panGesture setMaximumNumberOfTouches:1];
    [self.view addGestureRecognizer:panGesture];

    模糊图片的方法

    - (void)imageTaped:(UIPanGestureRecognizer *)gestureRecognizer
    {
    
        CIContext *context = [CIContext contextWithOptions:nil];
        CGPoint touchLocation = [gestureRecognizer locationInView:self.imgviewMain];
    
    
        CGRect temp=CGRectMake(touchLocation.x-25, ((self.imgviewMain.frame.size.height-50) - touchLocation.y)+25, 50, 50);
    
        CIImage *inputImage0 = [[CIImage alloc] initWithImage:self.imgviewMain.image];
    
        CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageWithCGImage:[context createCGImage:inputImage0 fromRect:temp]]];
        CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
        [blurFilter setDefaults];
        [blurFilter setValue: inputImage forKey: @"inputImage"];
        [blurFilter setValue: [NSNumber numberWithFloat:0.1]
                      forKey:@"inputRadius"];
    
    
        CIImage *outputImage = [blurFilter valueForKey: @"outputImage"];
        UIImageView *imgtest=[[UIImageView alloc]init];
    
        imgtest.image=[UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];
    
    
    
        UIImage *image;
    
        UIImage *bottomImage = self.imgviewMain.image; 
              image = imgtest.image;    
    
    
        CGSize newSize = CGSizeMake(self.imgviewMain.frame.size.width, self.imgviewMain.frame.size.height);
        UIGraphicsBeginImageContext( newSize );
    
        [bottomImage drawInRect:CGRectMake(0,0,768,1024)];
    
        CGRect newRect2=CGRectMake(temp.origin.x,((self.imgviewMain.frame.size.height-50) - temp.origin.y), image.size.width, image.size.height);
        image=[self makeRoundedImage:image radius:10];
    
        [image drawInRect:newRect2 blendMode:kCGBlendModeNormal alpha:0.5];
         UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        self.imgviewMain.image=newImage;
       }
    RoundedImage Method
    
    -(UIImage *)makeRoundedImage:(UIImage *) image
                          radius: (float) radius;
    {
        CALayer *imageLayer = [CALayer layer];
        imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        imageLayer.contents = (id) image.CGImage;
    
        imageLayer.masksToBounds = YES;
        imageLayer.cornerRadius = radius;
    
        UIGraphicsBeginImageContext(image.size);
        [imageLayer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return roundedImage;
    }
    2019-07-17 19:15:28
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
营销设计场景下的图像和文字生成 立即下载
复杂环境下的视觉同时定位与地图构建 立即下载
360°全景视频播放器的实现原理 立即下载