Author Archive

How to tint a UIButton in ios

 

 

 

 

UIImage *buttonImage = [UIImage imageNamed:@"myimage.png"];

buttonImage = [self tintTheImage:buttonImage tocolor:[UIColor redColor]];

[MyButton setImage:buttonImage forState:UIControlStateNormal];

-(UIImage *)tintTheImage:(UIImage *)image tocolor:(UIColor *)color

{

CGFloat scale = image.scale;

UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale));

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, image.size.height);

CGContextScaleCTM(context, 1.0, -1.0);

CGContextSetBlendMode(context, kCGBlendModeMultiply);

CGRect rect = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);

CGContextDrawImage(context, rect, image.CGImage);

// Create gradient

UIColor *colorOne = color;

UIColor *colorTwo = color;

NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, NULL);

// Apply gradient

CGContextClipToMask(context, rect, image.CGImage);

CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0,image.size.height * scale), 0);

UIImage *TintedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return TintedImage;

}

How to tint a UIImage in ios

 

 

-(UIImage *)tintTheImage:(UIImage *)image tocolor:(UIColor *)color

{

CGFloat scale = image.scale;

UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale));

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, image.size.height);

CGContextScaleCTM(context, 1.0, -1.0);

CGContextSetBlendMode(context, kCGBlendModeMultiply);

CGRect rect = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);

CGContextDrawImage(context, rect, image.CGImage);

// Create gradient

UIColor *colorOne = color;

UIColor *colorTwo = color;

NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, NULL);

// Apply gradient

CGContextClipToMask(context, rect, image.CGImage);

CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0,image.size.height * scale), 0);

UIImage *TintedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

 

return TintedImage;

}

First you need node and npm installed. Install it from nodejs.org.

To get setup with Apache Cordova run the following command in terminal

npm install -g cordova

To create a new project

In the terminal navigate to the folder where you want to create your project

Then run the following command

cordova create MyProject com.example.myproject

Here MyProject is the main folder that will contain all the files and subfolders of your project. Do not create this folder manually before executing the above command. The above command will create the MyProject folder and its content for you.

Next step is to create the android project. You will need the android sdk to run the android project.

To create the android project navigate to the MyProject folder in terminal.

Then run the following command

      cordova platform add android.

The above command will create the android project in the folder named ‘platforms’ which is a subfolder of the ‘MyProject’ folder

Parsing XML in ios

Can be done using NSXMLParser.

Create xml parser object with xml data, set its delegate , and call the parse method with parserObject.

Delegate methods getting called :
– parserDidStartDocument:
– parserDidEndDocument:
– parser:didStartElement:namespaceURI:qualifiedName:attributes:
– parser:didEndElement:namespaceURI:qualifiedName:
– parser:didStartMappingPrefix:toURI:
– parser:didEndMappingPrefix:
– parser:resolveExternalEntityName:systemID:
– parser:parseErrorOccurred:
– parser:validationErrorOccurred:
– parser:foundCharacters:
– parser:foundIgnorableWhitespace:
– parser:foundProcessingInstructionWithTarget:data:
– parser:foundComment:
– parser:foundCDATA:

How to get the coordinate of a UIview

The best practice is to use the bellow functions to get the coordinate of a uiview with respect to another

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

Saving images to database using sql in ios

// Create Table.
sql_statement = "CREATE TABLE IF NOT EXISTS IMAGES (IMAGE BLOB)";
if( sqlite3_exec(myDB, sql_statement, NULL, NULL, &errMsg) != SQLITE_OK)
    NSLog( @"Fail to create  table");

// funtion to save image
- (void) SaveImagesToDatabase: (NSData*) imageData
{
    const char* sqliteQuery = "INSERT INTO IMAGES (IMAGE) VALUES (?)";
    sqlite3_stmt* statement;

    if( sqlite3_prepare_v2(myDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK ) 
       { 
         sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT); 
         sqlite3_step(statement);
        } 
         else 
            NSLog( @"prepare failed");
 // Finalize and close database. 
      sqlite3_finalize(statement); 
}

Creating a singleton class in objective C

@implementation MySingleton static MySingleton* _sharedMySingleton = nil;
+(MySingleton*)sharedMySingleton
{
@synchronized([MySingleton class])
{
if (!_sharedMySingleton) [[self alloc] init];   return _sharedMySingleton;                       }
return nil;
}
+(id)alloc
{
@synchronized([MySingleton class])
{
NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second     instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}   return nil;
}
 
-(id)init
{
self = [super init];
if (self != nil)
{
// initializations
}
return self;
}

Reverse an nsarray in objective C

Let the array to be reversed is ‘original_array’
then you can obtain the reversed array as follows

NSArray* reversed_array = [[original_array reverseObjectEnumerator] allObjects];

Incoming search terms:

  • reverse nsarray xcode

To know how to get the latitude and longitude of user location in an iPhone app go to this link

We should have two variables for this purpose

CLGeocoder *geocoder;
CLPlacemark *placemark;

Don’t forget to initialize these variables before you use it anywhere in the code

We use the “reverseGeocodeLocation” method to translate the locate data into a human-readable address. The geocoding operations doesn’t happen on the device. The method instead submits the given data to the geocoding server in the cloud in order to resolve the address.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError*error) {

if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSString *address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];

NSLog(@”address = %@”, address);
}
} ];

}

Finding the User Location in iPhone App

1: Import the CoreLocation framework to the project

2: Add the delegate to the interface as follows

@interface MyLocationViewController : UIViewController<CLLocationManagerDelegate>

3: Declare and intialize a variable for handling the location(declare it in .h file and initialize it in viewdidload method)

CLLocationManager *locationManager;

locationManager = [[CLLocationManager alloc] init];

4: Now create the function which will get the current location as follows(here it is given as an IBAction)

- (IBAction)getCurrentLocation:(id)sender

{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];//The service will then continuously send your application a stream of location data.
}

5: Its not completed by the 4th step. We have to implement the delegate methods from which we will get the location. The two delegate methods we have to implement are as follows(the second method will get you the latitude and longitude as you will see)

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your        Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

NSLog(@"longitude=%@     latitude=%@", longitude, latitude);
}
Page 1 of 912345...Last »
We Can Build Your App for You. Lets Get Started !