Pages

Saturday, November 5, 2011

How to attach database with other database in SQLite

To Attach DB with another DB, i use below code................


Create first database:
[self createEditableCopyOfDatabaseIfNeeded];
Create second database:
[self createEditableCopyOfDatabaseIfNeeded_New];

Attach Database second with first:
[self AttachDB];
use of both data base in single SQL:
[self inserttblData];

-(voidAttachDB
{
               BOOL success;
               NSFileManager *fileManager=[[NSFileManager defaultManager]autorelease];
               NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        
               success = [fileManager fileExistsAtPath:[[paths objectAtIndex:0] stringByAppendingPathComponent:@"database2.sqlite3"]];
               if(success)
               {
                        NSString *sql = [NSString stringWithFormat:@"ATTACH DATABASE \"%@\" AS newdb",[[paths objectAtIndex:0] stringByAppendingPathComponent:@"database2.sqlite3"]];
                        // here database is an sqlite object and which refer database1
                        int intReturnValue = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);       
                        if(intReturnValue == 1)
                         {
                                    NSAssert1(0, @"Error: failed to attach database with message '%s'.", sqlite3_errmsg(database));
                        }
                        else
                        {
                                    NSLog(@"Done");  
                        }
               }   
}

-(int)inserttblData
{
               int i=0;
               @try
               {
                       
                        sqlite3_stmt *statement=nil;
                        NSString  *sql=nil;  
                        sql=[NSString stringWithFormat:@"INSERT INTO testtable1 SELECT * FROM newdb.testtable2"];
                        if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)!=SQLITE_OK)
                        {
                                    NSAssert1(0, @"Error: failed to insertstatement with message '%s'.", sqlite3_errmsg(database));
                        }                                  
                        int success=sqlite3_step(statement);
                        if (success == SQLITE_ERROR)
                        {
                                    NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
                        }
                        sqlite3_finalize(statement);
                       
                        return i;
                       
                       
               }
               @catch (NSException *e)
               {
                        return 0;
               }
               return i;
}

If you got any new way and get problem, write here.......

Thx,




Sunday, September 18, 2011

Add and Remove Tabbar item Run Time


In ios sdk, some time we need to insert/delete tab bar item run time. So In this case, we can add and remove tab bar items by following below code.


// Remove and Add Tab bar item from tabbar controller at run time:

-(IBAction) RemoveTab:(id)sender
{
               UIViewController *itemToRemove = nil;
   
        NSMutableArray *newItems = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
        for (UIViewController *aView in newItems)
        {
                       if ([[[aView class] description] isEqualToString:@"FirstViewController"])
            {
                //store this item to remove later
                         itemToRemove = aView;
            }
        }
        if (itemToRemove)
        {
            [newItems removeObject:itemToRemove];
              
        }
}


// Add  Tab bar item in tabbar controller at run time:

-(IBAction) AddTab:(id)sender
{
              
                   NSMutableArray *newItems = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
                       
//Add New ViewController

                   UITabBarItem *obj = [[FirstViewController alloc] init];
// or use below
               //UITabBarItem *obj = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

                   [newItems addObject:obj];
                        obj.title = @"First";
                        [obj release];
              
                        [self.tabBarController setViewControllers:newItems];

}


This code has worked perfectly for me, but if you have any problem inform me.
Or get any new or more efficient way.


Thanks……………..

HOW TO CREATE PUSH NOTIFICATION CERTIFICATE:



I follow the below link to create IOS push notification certificate.


Thanks……………………

Horizontal Picker

use below statement to make picker horizontal:

picker.transform = CGAffineTransformMakeRotation(M_PI+M_PI_2);
picker.frame = CGRectMake(80,10 ,150,44); 

Delegate and Data source function of picker:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{      
         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 20)];
        
         label.text = @"miles";
         label.textAlignment = UITextAlignmentCenter;
         label.textColor = [UIColor blackColor];
         label.font = [UIFont systemFontOfSize:13];
         label.backgroundColor = [UIColor clearColor];
         label.opaque = NO;
        
         CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57); 
        [label setTransform:rotate];
         return label;
        
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
         return 30;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
         return 80;
} 

Get String From HTML String



- (NSString *)flattenHTML:(NSString *)htmlstr
{
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString: htmlstr];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
htmlstr = [htmlstr stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:@"%@>", text]
withString:@" "];
} // while //
htmlstr=[htmlstr stringByReplacingOccurrencesOfString:@" " withString:@" "];
htmlstr=[htmlstr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
htmlstr=[htmlstr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" "];
htmlstr =[htmlstr stringByTrimmingCharactersInSet:set]retain];
return htmlstr;
}

How to Capture Video thumbnail from video URL



Include the Following Framework in project:
AVFoundation.framework
CoreMedia.framework


- (void) CaptureThumnailImageFromVideoURl
{
// NSURL *url = [NSURL URLWithString:@"http://videos.imotors.com/videos/1onONE/1onONE_intro_new.mp4"];
NSURL *url = [NSURL URLWithString:@"http://videos.imotors.com/videos/Top200/Top200_2010_Audi_A8.mp4"];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,15);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef img, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
thumbImageview.image = [UIImage imageWithCGImage: img];
[generator release];
};
CGSize maxSize = CGSizeMake(100, 90); // define the size of image.
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
}

Saturday, September 10, 2011

Combine Images In IOS


Combine two images:
UIImage *firstImage = [UIImage imageNamed:@"a.jpg"];

UIImage *secondImage = [UIImage imageNamed:@"b.jpg"];

CGSize size = CGSizeMake(firstImage.size.width+secondImage.size.width,firstImage.size.height);
UIGraphicsBeginImageContext(size);
[firstImage drawAtPoint:CGPointMake(0,0)];

[secondImage drawAtPoint:CGPointMake(firstImage.size.width,0)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
[self saveImage:newImage withName:@"ABC.png"];

UIGraphicsEndImageContext();

Store image in document.


-(void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data = UIImagePNGRepresentation(image);
// NSData *data = UIImageJPEGRepresentation(image, 1);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES)
objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}

RESULT:



Thanks.............