Image Processing : Image Colour Changed when Slider Value is Changed
Framework :
Core Image Framework added into Your project
New.h :
#import <UIKit/UIKit.h>
#import <CoreImage/CoreImage.h>
@interface New : UIViewController
{
UIImage *Imagesss;
IBOutlet UIImageView *theImageView;
IBOutlet UISlider *slider_hue;
}
@property (nonatomic, retain) IBOutlet UISlider *slider_hue;
@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)huechange:(id)sender;
-(IBAction)huevaluechange:(id)sender;
- (IBAction)original:(id)sender;
@end
New.m :
@synthesize theImageView, slider_hue;
- (void)viewDidLoad
{
[super viewDidLoad];
Imagesss = [UIImage imageNamed:@"hair.png"];
}
- (IBAction)original:(id)sender
{
theImageView.image = [UIImage imageNamed:@"hair.png"];
}
//Method 1
-(IBAction)huevaluechange:(id)sender
{
float f= slider_hue.value;
UIImage *image = [self imageWithImage:Imagesss rotatedByHue:f];
[theImageView setImage:image];
}
//Method 2
-(IBAction)huechange:(id)sender
{
float f= slider_hue.value;
UIImage *image = [self imageWithImage:[UIImage imageNamed:@"hair.png"] fixedHue:f alpha:1.0];
[theImageView setImage:image];
}
//Method 1 - Function
- (UIImage*) imageWithImage:(UIImage*) source rotatedByHue:(CGFloat) deltaHueRadians;
{
// Create a Core Image version of the image.
CIImage *sourceCore = [CIImage imageWithCGImage:[source CGImage]];
// Apply a CIHueAdjust filter
CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
[hueAdjust setDefaults];
[hueAdjust setValue: sourceCore forKey: @"inputImage"];
[hueAdjust setValue: [NSNumber numberWithFloat: deltaHueRadians] forKey: @"inputAngle"];
CIImage *resultCore = [hueAdjust valueForKey: @"outputImage"];
// Convert the filter output back into a UIImage.
// This section from http://stackoverflow.com/a/7797578/1318452
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef resultRef = [context createCGImage:resultCore fromRect:[resultCore extent]];
UIImage *result = [UIImage imageWithCGImage:resultRef];
CGImageRelease(resultRef);
return result;
}
//Method 2 - Function
- (UIImage*) imageWithImage:(UIImage*) source fixedHue:(CGFloat) hue alpha:(CGFloat) alpha;
{
// Note: the hue input ranges from 0.0 to 1.0, both red. Values outside this range will be clamped to 0.0 or 1.0.
// Find the image dimensions.
CGSize imageSize = [source size];
CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);
// Create a context containing the image.
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[source drawAtPoint:CGPointMake(0,0)];
// Draw the hue on top of the image.
CGContextSetBlendMode(context, kCGBlendModeHue);
[[UIColor colorWithHue:hue saturation:1.0 brightness:1.0 alpha:alpha] set];
UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
[imagePath fill];
// Retrieve the new image.
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
New.xib :
//Method 1 - Slider Value
slider value is range from 0 to 6
//Method 2 - Slider Value
slider value is range from 0 to 1
Output :
App Start Page |
Change the Slider Value |
No comments:
Post a Comment