2

我正在以编程方式创建一个CALayer子类,该子类对其自身应用一些像素噪声。该代码的工作原理是它在图层中呈现噪点,但图像上有一个奇怪的伪影,我无法确定根本原因。

这是一个示例图像,其中noiseOpacity出现了使问题更加明显。

在此处输入图像描述

粉红色的盒子是 a UANoisyGradientLayer,一个CAGradientLayer具有以下位的子类:

@interface UANoisyGradientLayer ()
    @property (nonatomic, retain) CIContext *noiseContext;
    @property (nonatomic, retain) CIFilter  *noiseGenerator;
    @property (nonatomic, retain) CIImage   *noiseImage;
@end

@implementation UANoisyGradientLayer

@synthesize noiseOpacity = _noiseOpacity, noiseImage;

- (id)init {
    self = [super init];
    if (self) {
        self.noiseOpacity = 0.10;
        self.noiseContext = [CIContext contextWithOptions:nil];         
        self.noiseGenerator = [CIFilter filterWithName:@"CIColorMonochrome"];
        [self.noiseGenerator setValue:[[CIFilter filterWithName:@"CIRandomGenerator"] valueForKey:@"outputImage"] forKey:@"inputImage"];
        [self.noiseGenerator setDefaults];

        self.noiseImage = [self.noiseGenerator outputImage];

    }
    return self;
}

- (void)drawInContext:(CGContextRef)ctx {

    [super drawInContext:ctx];

    CGRect extentRect = [self.noiseImage extent];
    if (CGRectIsInfinite(extentRect) || CGRectIsEmpty(extentRect)) {
        extentRect = self.bounds;
    }

    CGImageRef cgimg = [self.noiseContext createCGImage:self.noiseImage fromRect:extentRect];
    CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
    CGContextSetAlpha(ctx, self.noiseOpacity);
    CGContextDrawImage(ctx, self.bounds, cgimg);
    CGImageRelease(cgimg);
}

基本上,我使用 a作为过滤器的输入来创建CIImageinit 。然后,当需要绘制它时,我使用(范围始终为0)创建它,并将其绘制到上下文中。CIRandomGeneratorCIColorMonochromeCGImageRefself.bounds infinite

结果大部分都很好,但正如您在图像中看到的那样,似乎有一些拉伸正在发生。这里发生了什么?

4

1 回答 1

8

Although not fixing the original problem, I approached this from a different angle and have duplicated the output. Instead of trying to generate a single image the size of the self.bounds, I am now generating an image that is only 64x64, then tiling it using CGContextDrawTiledImage. Because I am now sizing it at a fixed size, I could pull some code out of the drawInContext: method. And finally, because there was no more image generation done in the draw methods, I was able to make it a static var so it is only ever generated once! Here is the complete class:

static CGImageRef   __noiseImage        = nil;
static CGFloat      __noiseImageWidth   = 0.0;
static CGFloat      __noiseImageHeight  = 0.0;

@implementation UANoisyGradientLayer

@synthesize noiseOpacity = _noiseOpacity;

- (id)init {
    self = [super init];
    if (self) {
        self.noiseOpacity = 0.1f;
        self.needsDisplayOnBoundsChange = YES;

        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            CIContext *noiseContext = [CIContext contextWithOptions:nil];

            CIFilter *noiseGenerator = [CIFilter filterWithName:@"CIColorMonochrome"];
            [noiseGenerator setValue:[[CIFilter filterWithName:@"CIRandomGenerator"] valueForKey:@"outputImage"] forKey:@"inputImage"];
            [noiseGenerator setDefaults];

            CIImage *ciImage = [noiseGenerator outputImage];

            CGRect extentRect = [ciImage extent];
            if (CGRectIsInfinite(extentRect) || CGRectIsEmpty(extentRect)) {
                extentRect = CGRectMake(0, 0, 64, 64);
            }

            __noiseImage = [noiseContext createCGImage:ciImage fromRect:extentRect];
            __noiseImageWidth = CGImageGetWidth(__noiseImage);
            __noiseImageHeight = CGImageGetHeight(__noiseImage);
        });
    }

    return self;
}

- (void)drawInContext:(CGContextRef)ctx {

    [super drawInContext:ctx];

    if (self.noiseOpacity > 0) {

        CGContextSaveGState(ctx);
        CGPathRef path = [[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.cornerRadius] CGPath];
        CGContextAddPath(ctx, path);
        CGContextClip(ctx);
        CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
        CGContextSetAlpha(ctx, self.noiseOpacity);


        CGContextDrawTiledImage(ctx, CGRectMake(0, 0, __noiseImageWidth, __noiseImageHeight), __noiseImage);

        CGContextRestoreGState(ctx);
    }
}

@end

NOTE: CIColorMonochrome and CIRandomGenerator require iOS 6 (or newer). Make sure to include the required frameworks (CoreImage.framework and QuartzCore.framework).

于 2012-07-04T21:43:57.207 回答