abcdeFGh_xyz 2013-01-06 03:48 采纳率: 69.2%
浏览 6795
已采纳

iphone中模糊图片的指定区域

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

  • 写回答

2条回答 默认 最新

  • redCoral_ 2013-01-06 06:04
    关注

    CSDN移动问答

    设置UIImageView的属性名字为:imageView,然后同一序列添加下面四个方法到实现文件中,然后设置ImageViewMode为Redraw,添加模糊效果的UIImage自定义或默认类,就OK了。

    method 1 - 剪裁图片

    #pragma mark - Croping the Image 
    
    - (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect{
    
        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);    
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
    
        return cropped;   
    
    
    }
    

    Method 2 -

    #pragma mark - Marge two Images 
    
    - (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect{
    
        CGSize size = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
        UIGraphicsBeginImageContext(size);
    
        CGPoint pointImg1 = CGPointMake(0,0);
        [img drawAtPoint:pointImg1]; 
    
        CGPoint pointImg2 = cropRect.origin;
        [img2 drawAtPoint: pointImg2];
    
        UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return result;
    }
    

    Method 3 - 圆角矩形

    #pragma mark - RoundRect the Image
    
    - (UIImage *)roundedRectImageFromImage:(UIImage *)image withRadious:(CGFloat)radious {
    
        if(radious == 0.0f)
            return image;
    
        if( image != nil) {
    
            CGFloat imageWidth = image.size.width;
            CGFloat imageHeight = image.size.height;
    
            CGRect rect = CGRectMake(0.0f, 0.0f, imageWidth, imageHeight);
            UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
            const CGFloat scale = window.screen.scale;
            UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
    
            CGContextRef context = UIGraphicsGetCurrentContext();
    
            CGContextBeginPath(context);
            CGContextSaveGState(context);
            CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
            CGContextScaleCTM (context, radious, radious);
    
            CGFloat rectWidth = CGRectGetWidth (rect)/radious;
            CGFloat rectHeight = CGRectGetHeight (rect)/radious;
    
            CGContextMoveToPoint(context, rectWidth, rectHeight/2.0f);
            CGContextAddArcToPoint(context, rectWidth, rectHeight, rectWidth/2.0f, rectHeight, radious);
            CGContextAddArcToPoint(context, 0.0f, rectHeight, 0.0f, rectHeight/2.0f, radious);
            CGContextAddArcToPoint(context, 0.0f, 0.0f, rectWidth/2.0f, 0.0f, radious);
            CGContextAddArcToPoint(context, rectWidth, 0.0f, rectWidth, rectHeight/2.0f, radious);
            CGContextRestoreGState(context);
            CGContextClosePath(context);
            CGContextClip(context);
    
            [image drawInRect:CGRectMake(0.0f, 0.0f, imageWidth, imageHeight)];
    
            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            return newImage;
        } 
        return nil;
    }
    

    Method 4 -触摸移动

    #pragma mark - Touch Methods
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    
        UIImage *croppedImg = nil;
    
        UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:self.imageView];
    
        double ratioW=imageView.image.size.width/imageView.frame.size.width ;
    
        double ratioH=imageView.image.size.height/imageView.frame.size.height;
    
        currentPoint.x *= ratioW;
        currentPoint.y *= ratioH;
    
        double circleSizeW = 30 * ratioW;
        double circleSizeH = 30 * ratioH;
    
    
        currentPoint.x = (currentPoint.x - circleSizeW/2<0)? 0 : currentPoint.x - circleSizeW/2;
        currentPoint.y = (currentPoint.y - circleSizeH/2<0)? 0 : currentPoint.y - circleSizeH/2;
    
    
        CGRect cropRect = CGRectMake(currentPoint.x , currentPoint.y,   circleSizeW,  circleSizeH);
    
        NSLog(@"x %0.0f, y %0.0f, width %0.0f, height %0.0f", cropRect.origin.x, cropRect.origin.y,   cropRect.size.width,  cropRect.size.height );
    
        croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
    
        // Blur Effect 
        croppedImg = [croppedImg imageWithGaussianBlur9];
    
        // Contrast Effect 
        // croppedImg = [croppedImg imageWithContrast:50];
    
    
    
        croppedImg = [self roundedRectImageFromImage:croppedImg withRadious:4]; 
    
        imageView.image = [self addImageToImage:imageView.image withImage2:croppedImg andRect:cropRect];  
    } 
    

    UIImage Category类

    UIImage+ImageBlur.h

    #import <UIKit/UIKit.h>
    
    @interface UIImage (ImageBlur)
    
    - (UIImage *)imageWithGaussianBlur9;
    
    @end
    

    UIImage+ImageBlur.m

    #import "UIImage+ImageBlur.h"
    
    @implementation UIImage (ImageBlur)
    
    - (UIImage *)imageWithGaussianBlur9 {
        float weight[5] = {0.1270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162};
        // Blur horizontally
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:weight[0]];
        for (int x = 1; x < 5; ++x) {
            [self drawInRect:CGRectMake(x, 0, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:weight[x]];
            [self drawInRect:CGRectMake(-x, 0, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:weight[x]];
        }
        UIImage *horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        // Blur vertically
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        [horizBlurredImage drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:weight[0]];
        for (int y = 1; y < 5; ++y) {
            [horizBlurredImage drawInRect:CGRectMake(0, y, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:weight[y]];
            [horizBlurredImage drawInRect:CGRectMake(0, -y, self.size.width, self.size.height) blendMode:kCGBlendModeNormal alpha:weight[y]];
        }
        UIImage *blurredImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        //
        return blurredImage;
    }
    
    @end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退