Jul 8 2010

Capture Save/Load/Remove Image in documents directory

NSFileManager offers a convenient way to write images to and load them from the documents directory.
If you’re frequently doing that in your project, I suggest to wrap up NSFileManager support in three simple methods:

Save/Load/Remove Image Methods

//saving an image

- (void)saveImage:(UIImage*)image:(NSString*)imageName {

NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

NSLog(@"image saved");

}

//removing an image

- (void)removeImage:(NSString*)fileName {

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];

[fileManager removeItemAtPath: fullPath error:NULL];

NSLog(@"image removed");

}

//loading an image

- (UIImage*)loadImage:(NSString*)imageName {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];

return [UIImage imageWithContentsOfFile:fullPath];

}

Now, you can easily save an image like:

[self saveImage: myUIImage: @"myUIImageName"];

or load it like:

myUIImage = [self loadImage: @"myUIImageName"];

or remove it like:

[self removeImage: @"myUIImageName"];

Capture an image

- (UIImage*) getGLScreenshot {
    NSInteger myDataLength = 320 * 480 * 4;

    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // gl renders "upside down" so swap top to bottom into new array.
    // there's gotta be a better way, but this works.
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    for(int y = 0; y <480; y++)
    {
        for(int x = 0; x <320 * 4; x++)
        {
            buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
        }
    }

    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * 320;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}

- (void)saveGLScreenshotToPhotosAlbum {
	UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);
}

Mar 10 2010

SQL Server – Clear Log File

sql server

After some research i found a very simple solution

Try following steps

1. change mode from FULL to Simple.
2. Right-click the DB & in shrink option, shrink only log files.
3. revert the mode to Full.

You will certainly see lot of difference in size of log files.


Feb 24 2010

SQL Server Perfomance Bible

sql server

Tips and tricks in SQL Server perfomance…. (read more)

Continue reading


Apr 25 2009

Repair or Fix Master boot Record using Os installation CDs

Problems with the master boot record (MBR) of a system may prevent the system from booting. The MBR may be affected by malicious code, become corrupted by disk errors, or be overwritten by other boot loaders when experimenting with multiple operating systems on a host. This recipe describes one method of repairing the MBR for an XP host using the recovery console.

Boot with the XP installation CD.

When prompted, press R to repair a Windows XP installation.

If repairing a host with multiple operating systems, select the appropriate one (XP) from the menu. If you have only one operating system, enter 1 to select it.

Enter the administrator password if prompted.

To fix the MBR, use the following command:

fixmbr

This assumes that your installation is on the C:\ drive. You will be presented with several scary warning lines the reading of which will make you want to say no. Microsoft is exceptionally vague regarding the conditions under which fixmbr can cause problems although they are clear about the consequences (losing all data on the hard drive), so use this at your own risk.

Type y and ENTER to fix the MBR.

Type exit to leave the recovery console and reboot.

In Windows Vista

You can automatically solve most startup problems with the Startup Repair tool. If you prefer a more manual approach, you can use the BootRec.exe tool in the Windows Recovery environment.

First, follow these steps to load the Windows Recovery Environment:

1. Restart the computer from the Windows Vista DVD (the computer must be configured to start from CD/DVD), and start setup.

2. Configure your language preferences.

3. When prompted, click Repair your computer.

click-repair-your-computer.png

4. Startup Repair will automatically detect a problem. When prompted, click Repair and restart, and then restart your computer from the Windows Vista DVD. If Startup Repair does not automatically detect a problem, continue to the next step.

5. When prompted, click Repair Your Computer.

6. Click Command Prompt from the System Recovery Options.

click-command-prompt.png

BootRec.exe provides the following command-line paramters:

  • /FixMbr. Re-writes the master boot record (MBR) of the system partition to start Windows Vista. This won’t overwrite the existing partition table.
  • /FixBoot. Writes a boot sector onto the system partition to start Windows Vista.
  • /ScanOs. Scans all disks for Windows Vista installations and displays them to you. Oddly, this didn’t work for me, even on a test system that was starting just fine.
  • /RebuildBcd. Scans all disks for Windows Vista installations and prompts you to pick the ones you want to add to the BCD.

Feb 19 2009

Windows Registry – Tips ‘n Tricks

imagesPlaying with windows registry…..

In this article I will show the steps to manually backup and restore the windows registry.
The reason of this article was the search a single registry entry done by a program.
Continue reading