Added UIImage Category #2

Closed
wants to merge 4 commits into
from

Projects

None yet

4 participants

@Vortec4800
Contributor

Added a quick category to apply the noise filter to any UIImage - good for buttons and other UI elements where an image is used.

@kgn
Owner
kgn commented Sep 14, 2012

Thanks for the pull request! Maybe I'm over looking a use case but this doesn't seem to useful to me because buttons and other UI elements usually have alpha channels. A button for example usually has round edges, this code would draw noise over the entire image including the transparent part outside the rounded edge. Could you please provide an example of how you've used this, or see it being used?

This image has opacity, but the noise is drawn over the entire image bounds: http://cl.ly/image/260B2I1I3x3g
Button image used, with @2x added to the name: http://upload.wikimedia.org/wikipedia/commons/c/c9/Perspective-Button-Go-icon.png

I have used KGNoise in conjunction with other drawing code and my UIImage+BBlock category, but for things like buttons and other shapes I've always used [path addClip]; so the noise only draws within a shape.

Alpha masking could be added to this code, but I am not sure of the usefulness of having this in the main repo.

@kgn
Owner
kgn commented Sep 14, 2012

This code is also not retina aware, it would need to account for image scale like this:

Before: http://cl.ly/image/3b3S2s450D3P
After: http://cl.ly/image/290p0U3i1K0I

- (UIImage *)imageWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{
    // Create a context to draw in
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 self.size.width * self.scale,
                                                 self.size.height * self.scale,
                                                 8, /* bits per channel */
                                                 (self.size.width * self.scale * 4), /* 4 channels per pixel * numPixels/row */
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorSpace);

    // Flip context
    CGContextTranslateCTM(context, 0, self.size.height * self.scale);
    CGContextScaleCTM(context, self.scale, -self.scale);

    UIGraphicsPushContext(context);

    // Draw the image
    [self drawAtPoint:CGPointMake(0.0, 0.0)];

    // Noise on top
    [KGNoise drawNoiseWithOpacity:opacity andBlendMode:blendMode];

    // Create a CGImage from the context
    CGImageRef rawImage = CGBitmapContextCreateImage(context);
    UIGraphicsPopContext();
    CGContextRelease(context);

    // Create a UIImage from the CGImage
    UIImage *finishedImage = [UIImage imageWithCGImage:rawImage scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(rawImage);

    return finishedImage;
}
@chbeer
chbeer commented Sep 14, 2012

Would be useful to create a UIColor with noise as a pattern. Maybe a UIColor category would be useful, too :)

@kgn
Owner
kgn commented Sep 14, 2012

NS/UIColor category could be useful, added #3

@Vortec4800
Contributor

That's funny you should mention - my first intention was to make a UIColor category with the idea that you could generate a UIColor with noise and apply it as a tint to a navigation bar. I got it all working but the issue was that the tintColor property doesn't accept a UIColor from a pattern so it didn't really work, so I adapted the code for UIImage instead. The images I was testing with didn't have any alpha but I can see how that would be a problem. I'll look into that.

Many times a client will send us images for buttons and other things (backgrounds, etc) and we'd like to apply noise to it, so I figured a UIImage category might be useful for that. If you want to play around with my UIColor stuff more I can commit that too.

@kgn
Owner
kgn commented Sep 20, 2012

Hey @Vortec4800 I'll be merging this in but I want to make sure the library has both Mac and iPhone support so I'll need to carve out some time to get what you have running on the Mac too. Thanks for this work!

@Vortec4800
Contributor

Sounds like a good policy. I don't really do Mac development so can't help you too much with that part. :)

@kgn kgn referenced this pull request Sep 21, 2012
Closed

NS/UIColor category #3

@kgn kgn added a commit that referenced this pull request Nov 9, 2012
@kgn Adding color categories: #2 8859b94
@machizhao

.

@rastersize rastersize added a commit to rastersize/KGNoise that referenced this pull request Dec 29, 2012
@rastersize rastersize Merge remote-tracking branch 'upstream/master'
* upstream/master:
  Adding color categories: #2
  improved the radial gradient radius code based on @marcosanson pull request
  updating screenshot
  renaming screenshot and updating readme
  moving the default image into the iOS group
  adding KGNoiseLinearGradientView and KGNoiseRadialGradientView closes #5
  adding Default-568h@2x.png
  Minor edit to readme.md
2fe5e3f
@kgn kgn added a commit that closed this pull request Feb 15, 2013
@kgn Added the image noise categories to the main KGNoise file so there is…
… only one thing to add. Also implemented the image category for NSImage and added examples to the sample apps.

fix #2
d43aecf
@kgn kgn closed this in d43aecf Feb 15, 2013
@kgn
Owner
kgn commented Feb 15, 2013

@Vortec4800 Sorry for the delay, finally got this in NSImage support and examples. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment