Monday, August 30, 2010

Merging Layers of image in iPhone

CGImageRef _backgroundImageViewRef = _backgroundImageViewInTweek.image.CGImage;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef contextRef = CGBitmapContextCreate(NULL, _backgroundImageViewInTweek.image.size.width, _backgroundImageViewInTweek.image.size.height,
8, _backgroundImageViewInTweek.image.size.width * 4,
colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, _backgroundImageViewInTweek.image.size.width, _backgroundImageViewInTweek.image.size.height), _backgroundImageViewRef);
CGContextSetLineWidth(contextRef, 4);
CGContextSetRGBStrokeColor(contextRef, 0.0, 0.0, 1.0, 0.5);
CGRect costume_rect = CGContextConvertRectToDeviceSpace(contextRef, CGRectMake(0.0f,0.0f,171, 217));
CGContextDrawImage(contextRef, costume_rect, _costumeImageViewInTweek.image.CGImage); 
UIImage *_mergingBackgroundAndCostumeImage;
_mergingBackgroundAndCostumeImage =[UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)];

CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

With Regards,
Dinesh Sharma

Wednesday, August 18, 2010

color code detection in VB

 Public Function ConvertToGrayscale(ByVal source As Bitmap) as Bitmap

  Dim bm as new Bitmap(source.Width,source.Height)

  Dim x

  Dim y

  For y=0 To bm.Height

    For x=0 To bm.Width

      Dim c as Color = source.GetPixel(x,y)

      Dim luma as Integer = CInt(c.R*0.3 + c.G*0.59 + c.B*0.11)

      bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma)

    Next

  Next

  Return bm

End Function

Wednesday, August 4, 2010

How to Mask an Image

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage; 
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}

Tuesday, August 3, 2010

Crop an image using the iPhone SDK

One of the pieces of Game Trivia Catechism that I've been putting off for some time is the timer displayed on the question screen.  The timer is basically a reverse progress indicator.  It starts out full (with all of the lights on) and slowly counts down to empty (all lights off).  The simplest method I know of for building a progress indicator is to use two images (one full, one empty) and simply crop the "full" to an appropriate sized based on the current progress position.
When it came time to implement this in GTC, I scoured the Cocoa Touch documentation for an easy way to crop a UIImage down to a specific size.  Apparently this was something that didn't make the cut.  Fortunately the lower level CoreGraphics methods provide a fairly simple method of doing this.  Here's what I ended up with.
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
   //create a context to do our clipping in
   UIGraphicsBeginImageContext(rect.size);
   CGContextRef currentContext = UIGraphicsGetCurrentContext();
   //create a rect with the size we want to crop the image to
   //the X and Y here are zero so we start at the beginning of our
   //newly created context
   CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
   CGContextClipToRect( currentContext, clippedRect);
   //create a rect equivalent to the full size of the image
   //offset the rect by the X and Y we want to start the crop
   //from in order to cut off anything before them
   CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                rect.origin.y * -1,
                                imageToCrop.size.width,
                                imageToCrop.size.height);
   //draw the image to our clipped context using our offset rect
   CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
   //pull the image from our cropped context
   UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
   //pop the context to get back to the default
   UIGraphicsEndImageContext();
   //Note: this is autoreleased
   return cropped;
}

Then call the code with something like:
- (void)drawRect:(CGRect)rect
{
   //draw the whole lights off image
   //the on images will be drawn overtop
   [lightsOffImage drawInRect:rect];
   //if we don't have any lights on... no point in continuing
   if( numberOfLightsOn < 1 )
     return;
   //figure out the dimensions of numberOfLights on bulbs
   CGSize croppedSize = CGSizeMake(LIGHT_WIDTH * numberOfLightsOn, LIGHT_HEIGHT);
   CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);
   //get the "on" bulbs by cropping the image
   UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];
   //create a rect to draw the newly cropped on images to
   CGRect lightsOnRect = CGRectMake(LIGHT_BULB_OFFSET_X,
                                    LIGHT_BULB_OFFSET_Y,
                                    croppedSize.width,
                                    croppedSize.height);
   //draw the "on" lights
   [cropped drawInRect:lightsOnRect];
   //cropped is autoreleased so no need to worry about cleanup
}
Regards,
dinesh

Monday, July 12, 2010

A Quick Look at Model-View-Presenter (MVP) Architecture


Some noteworthy benefits of MVP pattern:

  • Platform independent: In this Pattern application make independent from its platform so when ever requirement comes to move to window to web or any other version its only need to change on code behind page for view Implementation, so later on required to any of the application portion convert in web to window or any other platform we can handle it by minimum efforts.
  • Loose couples form UI: So application stays loose couples from code behind page (UI) to business layer or any other next latter.
  • Useful to create automated unit test: MVP is purely loose couples from any plate form so that it is very use to prepare automated unit test cases.
  • Increase code maintainability: Keeps code more maintainable throughout the lifetime of the project, especially during the maintenance phase.

Overall Application Architecture:

    1.  Model: Model is UI interface for how and what the data are needed to displayed.
    2.  View: Show the representation of model, that can be build in presenter
    3.  Presenter: Build all data for model and pass all data to model in form of view by communicating with services class( or business logic layer)

    Summary:

    • MVP is little prolonged process to implement and understanding in the beginning, but later on it will be very useful as it provides application maintainability on application maintenance phase, increase code re-usability. A good thing about it is a clear distinction of different layers.  For example, all the code is separate from the user interface.
    • MVP is a strong consideration candidate for large scale projects especially where project maintainability re-usability are the key success criteria. It suits well with automated test cases methods also.

Tuesday, July 6, 2010

Download an Image and Save it as PNG or JPEG in iPhone SDK

code : 

NSLog(@"Downloading...");
 // Get an image from the URL below
 UIImage *image = [[UIImage alloc] initWithData:[NSData 
dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.objectgraph.com/images/og_logo.png"]]];
 
 NSLog(@"%f,%f",image.size.width,image.size.height);
 
 // Let's save the file into Document folder.
 // You can also change this to your desktop for testing. (e.g. 
/Users/kiichi/Desktop/)
 // NSString *deskTopDir = @"/Users/kiichi/Desktop";
 
 NSString *docDir = [NSSearchPathForDirectoriesInDomains(
   NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 
 // If you go to the folder below, you will find those pictures
 NSLog(@"%@",docDir);
 
 NSLog(@"saving png");
 NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
                [data1 writeToFile:pngFilePath atomically:YES];
 
 NSLog(@"saving jpeg");
 NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
 NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 
1.0f)];//1.0f = 100% quality
 [data2 writeToFile:jpegFilePath atomically:YES];
 
 NSLog(@"saving image done");
 
 [image release];







important link:

http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/

Friday, July 2, 2010

Add and Get double value or numbers in a mutable array in iphone.

        
        code here :


        NSString *str = @"1.22222222";
double d = [str doubleValue];
arrForDoubleValue = [[NSMutableArray alloc] init];
[arrForDoubleValue addObject:[NSNumber numberWithDouble:d]];
for(int i = 0; i<[arrForDoubleValue count];i++)
{
double a = [[arrForDoubleValue objectAtIndex:i]doubleValue];
NSLog(@"%.8lf",a);
}