将遮罩应用于iOS中的blurView

时间:2013-09-21 06:14:06

标签: ios imageview mask blur

我有一个图像,我正在尝试应用堆栈模糊,并希望应用我创建的.png文件中的掩码,以查看下面的清晰图像。我需要下面的图像才能在给定时间更换,但带有遮罩的blurView保持静止。这是我正在使用的代码,但我没有在模糊中获得蒙版图像。

  - (void)viewDidLoad
    {
        [super viewDidLoad];

    UIImageView *imageview = [UIImageView new];
    [imageview setFrame:[self.view bounds]];
    [imageview setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [imageview setImage:[UIImage imageNamed:@"myImage.jpg"]];
    [imageview setContentMode:UIViewContentModeScaleAspectFill];
    [self.view addSubview:imageview];

    [self setBlurView:[AMBlurView new]];
    [[self blurView] setFrame:CGRectMake(0.f, 0.f, [self.view bounds].size.width, [self.view bounds].size.height)];
    [[self blurView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

    [self.view addSubview:[self blurView]];

    imageview.image = [self maskImage:imageview.image withMask:[UIImage imageNamed:@"mask"]];

}

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef imgRef = [image CGImage];
    CGImageRef maskRef = [maskImage CGImage];
    CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                              CGImageGetHeight(maskRef),
                                              CGImageGetBitsPerComponent(maskRef),
                                              CGImageGetBitsPerPixel(maskRef),
                                              CGImageGetBytesPerRow(maskRef),
                                              CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
    return [UIImage imageWithCGImage:masked];

}

1 个答案:

答案 0 :(得分:2)

尝试使用此可可控件进行模糊图像

COCOA CONTROL

UpDated Answer

- (void)viewDidLoad
    {
        [super viewDidLoad];

    UIImageView *imageview = [UIImageView new];
    [imageview setFrame:[self.view bounds]];
    [imageview setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [imageview setImage:[UIImage imageNamed:@"myImage.jpg"]];
    [imageview setContentMode:UIViewContentModeScaleAspectFill];
 imageview.image = [self maskImage:imageview.image withMask:[UIImage imageNamed:@"mask"]];
    [self.view addSubview:imageview];

    [self setBlurView:[AMBlurView new]];
    [[self blurView] setFrame:CGRectMake(0.f, 0.f, [self.view bounds].size.width, [self.view bounds].size.height)];
    [[self blurView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

    [self.view addSubview:[self blurView]];



}

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef imgRef = [image CGImage];
    CGImageRef maskRef = [maskImage CGImage];
    CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                              CGImageGetHeight(maskRef),
                                              CGImageGetBitsPerComponent(maskRef),
                                              CGImageGetBitsPerPixel(maskRef),
                                              CGImageGetBytesPerRow(maskRef),
                                              CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
    return [UIImage imageWithCGImage:masked];

}