Thursday, June 30, 2011

Resize UIImage tutorial

-(UIImage*) scaleAndRotateImage:(UIImage*)photoimage width:(CGFloat)bounds_width height:(CGFloat)bounds_height;
{
    CGImageRef imgRef = photoimage.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTrans
form transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);

    bounds.size.width = bounds_width;
    bounds.size.height = bounds_height;

    CGFloat scaleRatio = bounds.size.width / width;
    CGFloat scaleRatioheight = bounds.size.height / height;
    trans
form = CGAffineTransformIdentity;

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
    CGContextTranslateCTM(context, 0, -height);

    CGContextConcatCTM(context, trans
form);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}

Objective -C Date Formatter Examples - stringFromDate



The format specifiers are quite straightforward, Y = year, M = month, etc. Changing the number of specifiers for a field, changes the output. For example, MMMM generates the full month name “November”, MMM results in “Nov” and MM outputs “11″.
What follows is an example to create the following date string:
Saturday November 8, 2008:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:today];
[dateFormat release];
Here’s another example showing the current time
9:20 AM, PST:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"h:mm a, zzz"];
NSString *dateString = [dateFormat stringFromDate:today];
[dateFormat release];

More details

http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-2.html

Simple NSThread Example


Threading

Sample Format:
Step .1
[NSThread detachNewThreadSelector:@selector(myMethod) toTarget:self withObject:nil];

Step .2
- (void)myMethod { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

*** code that should be run in the new thread goes here ***


[pool release]; }

Step .3
[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:false];

Creating the Slider




To create a slider, you allocate it and initialize it with a frame just like any other UIKit control:

slider = [[UISliderControl alloc] initWithFrame: CGRectMake(0.0f, 32.0f, 320.0f, 20.0f)];

Next, you specify its minimum and maximum values with the setMinValue: and setMaxValue: calls. The "setShowValue" call is optional. When set to YES, it displays the current value to the right of the slider (as shown here) so the user gets instant feedback.

[slider setMaxValue:100.0f];
[slider setMinValue:0.0f];
[slider setShowValue:YES];

Now you must add a target for events. Here, I tell the slider to issue the handleSlider: method for mouse dragged events. Fortunately, the iPhone seems to use standard NextStep event equivalents, so 7 corresponds to NSRightMouseDragged, 1 to NSLeftMouseDown and 2 to NSLeftMouseUp. Since the iPhone only supports one button, the right and left gets a little confused.

[slider addTarget:self action:@selector(handleSlider:) forEvents:7];

Next, add the behavior for the message that gets called. In this example, I just write the [slider value] to a text view.

- (void) handleSlider:(id)sender{
[textView setText:[NSString stringWithFormat:@"End Value: %f", [slider value]]];

}



Reference:
// http://www.tuaw.com/2007/09/04/iphone-coding-using-the-slider/

Difference between NSString and NSMuttableString



//  Setup  two  variables  to  point  to  the  same  string
NSString * str1 = @"Hello World";

NSString * str2 = str1;


//  "Replace"  the  second  string
str2 = @"Hello ikilimnik";


//  And  list  their  current  values
NSLog(@"str1 = %@, str2 = %@", str1, str2);


Mutable strings


//  Setup  two  variables  to  point  to  the  same  string
NSMutableString * str1 = [NSMutableString stringWithString:@"Hello World"];

NSMutableString * str2 = str1;


//  "Replace"  the  second  string
[str2 setString:@"Hello ikilimnik"];


//  And  list  their  current  values
NSLog(@"str1 = %@, str2 = %@", str1, str2);


Notice when you use the immutable NSString class that the only way to "replace" a string is to create a new string and update your variable "str2" to po
int to it. 
This however doesn't affect what "str1" is pointing to, so it will still reference the original string.


In the NSMutableString example, we don't create a second string, but instead alter (mutate) the contents of 

the existing "Hello World" string. Since both variables continue to po
int to the same string object, they will both report the new value in the call to NSLog.

It's important to differentiate between a pointer variable and the actual object it points to. 

A NSString object is immutable, but that doesn't stop you from changing the value of a variable which points to a string.


The data type "NSString *" is a pointer to a NSString object, not the object itself. 

If you set a break po
int at either of the NSLog statements within the XCode debugger, you may like to check the raw value of each variable to clarify this.


Reference:


http:
//www.cocoadev.com/index.pl?NSString

http:
//stackoverflow.com/questions/1805442/nsstring-immutable-allows-to-change-its-values

Wednesday, June 29, 2011

how to use SwipeGestureRegognizer

// add the below code in viewDidLoad  method
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(swipeMethod:)];
    swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];

-(void)swipeMethod: (UISwipeGestureRecognizer *) sender
{
NSLog(@"swipe event detected");

}

// if we need means we use separate swipegesture for left, right, up and down direction

Touch (XY) detection in iPhone

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    startTime = [NSDate timeIntervalSinceReferenceDate];
     pt = [[touches anyObject] locationInView:self.view];
    NSLog(@"%d,%d x y",pt.x,pt.y);
  }

Regarding This Blog

This Blog is only use for study related . if you have any question just update me i will help you .

Tuesday, June 28, 2011

To Rotate a view

       //to rotae a view
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(180);// angle of rotation
        self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
        self.view.center = CGPointMake(160.0f, 240.0f);
        NSLog(@"end");
        [UIView commitAnimations];

Monday, June 27, 2011

Milli Seconds calculation

       // Start timer
    NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
   
      // Do something
    for(double i=0; i < 1000000; i++ );
   
       // Stop timer
    NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate];
   
       // Get the elapsed time in milliseconds
    NSTimeInterval elapsedTime = (endTime - startTime) * 1000;

       // Send it to the Console
    NSLog(@"Elapsed time in ms: %f", elapsedTime);

Thursday, June 23, 2011

Memory Management Simple Example

// string1 will be released automatically
  NSString* string1 = [NSString string];
// must release this when done
  NSString* string2 = [[NSString alloc] init]; [string2 release];

Network Activity Status Indicator


UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
// to stop it, set this to NO

Json Parsing Request

NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
                       
                        @"some value", @"key1",
                       
                        @"another value", @"key2",
                       
                        nil];
   
   
    NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
                       
                        @"yet another value", @"key1",
                       
                        @"some other value", @"key2",
                       
                        nil];
   
   
    NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];
   
   
    NSString *jsonString = [array JSONRepresentation];
   
    NSLog(@"%@",jsonString);
   
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://sample.com"]];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
   
    NSLog(@"%@",connection);



Note:

Before write code you have to include JSON Files. it is available

If you have any doubt ask me...

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...