Wednesday, 19 March 2014

Image Processing : Image Colour Changed

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


AppDelegate Method - Store a Variable (Singleton)

AppDelegate.h :
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
 NSString *name;
}
@property(strong,nonatomic)NSString *name;

@end

AppDelegate.m :
 //Nothing Do

SomeFile.h :
#import "AppDelegate.h"
@interface SomeFile : UIViewController
{
    
}
-(IBAction)save:(id)sender;
-(IBAction)retrive:(id)sender;
@end

SomeFile.m :
-(IBAction)save:(id)sender
{
      AppDelegate *del=(AppDelegate *)[[UIApplication sharedApplication]delegate];
      [del setName:txt_name.text];
}

-(IBAction)retrive:(id)sender
{
      AppDelegate *del=(AppDelegate *)[[UIApplication sharedApplication]delegate];
     if (del.name==nil)
     {
           // Login page is showed
     }
     else
     {
           NSString *name = del.name; 
     }
}

Singleton Object Creation

Singleton Object Creation


Defn:


In computer programming a singleton variable is a variable that is referred to only once.
Examples of where a variable might only be referenced once is as a dummy argument in a function call, or when its address is assigned to another variable which subsequently accesses its allocated storage.
Singleton variables sometimes occur because a mistake has been made – such as assigning a value to a variable and forgetting to use it later, or mistyping one instance of the variable name. Some compilers and lint-like tools flag occurrences of singleton variables.

Creating Singleton Object File:

In Sample Project we can add the file in Xcode tool.

File --> New --> File --> Choose Objective-C class --> Next --> 

class Name is "singleton" 
subclass is NSObject is Selected.

Next --> Finish.


singleton.h :

 #import <Foundation/Foundation.h>

@interface Singleton : NSObject

//Create the variables or objects.
@property(nonatomic,assign) float ad_height;
@property(nonatomic,assign) BOOL ads_removed,is_ipad;
@property(nonatomic, assign) CGRect banner_land;
@property(nonatomic, assign) CGRect banner_port;
@property(nonatomic,assign) UIInterfaceOrientation last_orient;


@property(nonatomic,assign) NSString *lastsss;

+(Singleton *)singleObj;  //class method


//-(void)Objsin;   //instance method

@end

singleton.m :

 #import "Singleton.h"

@implementation Singleton

@synthesize lastsss;

+(Singleton *)singleObj     //class Method
{
    
    static Singleton * single=nil;
    
    @synchronized(self)
    {
        if(!single)
        {
            single = [[Singleton alloc] init];
        }
    }
    return single;
}


-(void)Objsin      //instance method
{
    lastsss = @"aaa";

}


@end


SomeFile.h :


#import "Singleton.h"

@interface SomeFile : UIViewController
{
    Singleton *sobj;
}


SomeFile.m :

  //public method. Access from any page. Deallocate when app is closed.
-(void) viewwillappear method
{
   //reference to allocate
   sobj = [Singleton singleObj];    

   //Method calling
   [sobj Objsin];

    //Assign the values
    NSString *str= @"bb";
    sobj.lastsss = str;

    sobj.last_orient = [UIApplication sharedApplication].statusBarOrientation;

    sobj.banner_land = _bannerView.frame;   //CGRect(0, 0, 0, 0)
    sobj.banner_port = _bannerView.frame;   //CGRect(0, 0, 0, 0)

    sobj.ad_height = 40.0;

    sobj.is_ipad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);    //YES or NO

    sobj.ads_removed = NO;
}

               (or)

 //private method. Access only this page. Deallocate when page is moved.
-(void) viewwillappear method
{
    //initiate memory allocation
    sobj = [[Singleton alloc]init];

    //Method calling
    [sobj Objsin]; 


    //Assign the values
    NSString *str= @"bb";
    sobj.lastsss = str;

    sobj.last_orient = [UIApplication sharedApplication].statusBarOrientation;

    sobj.banner_land = _bannerView.frame;   //CGRect(0, 0, 0, 0)
    sobj.banner_port = _bannerView.frame;   //CGRect(0, 0, 0, 0)

    sobj.ad_height = 40.0;

    sobj.is_ipad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);    //YES or NO

    sobj.ads_removed = NO;
}