Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:14:11+00:00 2026-06-07T02:14:11+00:00

I am programmatically creating a CALayer subclass which applies a bit of pixel noise

  • 0

I am programmatically creating a CALayer subclass which applies a bit of pixel noise to itself. The code works in that it renders noise in the layer, but there is a strange artifact on the image that I am unable to determine the root cause.

Here is a sample image with the noiseOpacity turned up to make the problem more visible.

enter image description here

The pink box is a UANoisyGradientLayer, a CAGradientLayer subclass with the following bits:

@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);
}

Basically, I create the CIImage in init using a CIRandomGenerator as input to a CIColorMonochrome filter. Then, when it comes time to draw it, I create a CGImageRef out of it using self.bounds (the extent is always infinite or 0), and draw it to the context.

The result is mostly fine, but as you can see in the image, there seems to be some stretching going on. What is happening here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:14:13+00:00Added an answer on June 7, 2026 at 2:14 am

    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 64×64, 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm programmatically creating an edmx file as part of our code generation process and
I have a scrollview in which i am creating buttons programmatically. On touch, button
I am creating a viewController programmatically (hopefully the right way) My problem is that
I'm creating a UIDatePicker programmatically, and setting its locale with the following code: datePicker.locale
I'm using a UIImageView as the accessoryView in a UITableViewCell that I'm creating programmatically.
I am creating a view controller programmatically which is a UITabBarDelegate and contains a
I'm creating an interface programmatically that contains a grid of cells. I'd like the
I am programmatically creating an Ellipse shape but I can't find any property that
I have the following app, that's like a chat, so I'm programmatically creating some
I've got a UITabBarController in my project that I'm creating programmatically - without a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.