Saturday, June 9, 2012

Remove .svn file from your project

Terminal command to remove the .svn files from your project.

find . -name ".svn" -type d -exec rm -rf {} \;

usage: 
go to your project directory by making a 'cd'.
 cd /..../..../myproject

execute the command.
find . -name ".svn" -type d -exec rm -rf {} \;

thats it....simple and very effective :)



svnX FileMerge does not work with XCode 4.3 upgrade

Many of us who upgraded their XCode to 4.3 must be facing an issue with FileMerge application which is part of svnX. After the XCode 4,3 upgrade, FileMerge application location has been changed. This is why it fails to launch from svnX. 


You can fix this running the following command on terminal.
sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer
Also, you can launch the FileMerge directly from Xcode.
Xcode -> Open Developer Tool -> FileMerge. 

The application binary can be found here:
 /Applications/Xcode.app/Contents/Applications/FileMerge.app.

Sunday, June 3, 2012

UIImage From Movie File

Simple utility method to create a image from video file. Specify the size of the image and the time interval.

UIImage *getImageFromMovieAtTime(AVURLAsset *videoAsset, CGSize imageSize,CMTime frameTime)
{
    NSError *error =nil;
    CMTime actualTime;
    
    
    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:videoAsset];
    imageGenerator.appliesPreferredTrackTransform = YES;
    imageGenerator.maximumSize = imageSize;
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:frameTime actualTime:&actualTime error:&error];
    UIImage *movImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    
    if(error || movImage == nil)
    {
         DEBUGLOG(@"Error: %@",error);
    }
    DEBUGLOG(@"imagesize: %@",NSStringFromCGSize(movImage.size));
 
 return movImage;
}
Usage:
    UIImage *videoFrame = getImageFromMovieAtTime(videoAsset, CGSizeMake(width, height),kCMTimeZero);

Monday, May 28, 2012

Launching Map App on iOS to show route between source and destination location


+(void)showRouteOnMapFrom:(CLLocationCoordinate2D)sourceCoordinate to:(CLLocationCoordinate2D) destCoordinate
{
    NSString* urlString = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
                           sourceCoordinate.latitude, sourceCoordinate.longitude,destCoordinate.latitude, destCoordinate.longitude];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}

Saturday, May 26, 2012

Enable ARC in a Non ARC project

Small trick to enable ARC on a specific file in a non-ARC project by using the flag -fobjc-arc

Sunday, March 25, 2012

Documents Directory

Simple C function to retrive the path of the documents directory in iOS

NSString *documentsDirectory(void)
{
 static NSString *docPath = nil;
 if(docPath == nil)
 {
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  docPath = [[paths objectAtIndex:0] retain];
 }
    return docPath;
}

Thursday, September 30, 2010

Create Movie Using AVFoundation

Steps to Create Movie Using AVFoundation:-
1. Create a AVMutableComposition
CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent(); //Debug purpose - used to calculate the total time taken
NSError *error = nil;
AVMutableComposition *saveComposition = [AVMutableComposition composition];
2. Get the video and audio file path
NSString *tempPath = NSTemporaryDirectory();
NSString *videoPath = nil ;//<Video file path>;
NSString *audioPath = nil ;//<Audio file path>;;
3. Create the video asset 
NSURL * url1 = [[NSURL alloc] initFileURLWithPath:videoPath];
AVURLAsset *video = [AVURLAsset URLAssetWithURL:url1 options:nil];
[url1 release];
4. Get the AVMutableCompositionTrack for video and add the video track to it.
The method insertTimeRange: ofTrack: atTime: decides the what portion of the video to be added and also where the video track should appear in the final video created.
AVMutableCompositionTrack *compositionVideoTrack = [ addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[video tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [video duration])  ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
NSLog(@"%f %@",CMTimeGetSeconds([video duration]),error);

5. Create the Audio asset 
NSURL * url2 = [[NSURL alloc] initFileURLWithPath:audioPath];
AVURLAsset *audio = [AVURLAsset URLAssetWithURL:url2 options:nil];
[url2 release];

6. Get the AVMutableCompositionTrack for audio and add the audio track to it.
AVMutableCompositionTrack *compositionAudioTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipAudioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [audio duration])  ofTrack:clipAudioTrack atTime:kCMTimeZero error:nil];
NSLog(@"%f %@",CMTimeGetSeconds([audio duration]),error);
7. Get file path for of the final video.
NSString *path = [tempPath stringByAppendingPathComponent:@"mergedvideo.mov"];
if([[NSFileManager defaultManager] fileExistsAtPath:path])
{
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

8. Create the AVAssetExportSession and set the preset to it.
The completion handler will be called upon the completion of the export.
AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality] autorelease];
exporter.outputURL=url;
exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0];
[exporter exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"export completed : %lf",CFAbsoluteTimeGetCurrent()-currentTime);
}];

The above technique can be used to merge N number of video and audio tracks and create a movie. I hope this tutorial helps to create movie using AVFoundation.