How-To: Check root DNS settings for domain

Using “dig” you can determine what IP address or CNAME your domain is pointing to quite easily. However what I didn’t know was that you can actually check what the root server thinks your domain is pointing at, so you can check if its set correctly when you are migrating to a new IP address.

To do this, simply type:

dig domaintolookup.com +trace

How-To: Flip Rows/Columns in Excel

This is really easy, but something I never knew until I just tried this:

  1. Copy the cells you want to flip
  2. Right-Click “Paste Special”
  3. Tick the Transpose checkbox

You’re done! Best Excel trick ever!

How-To: Create my.cnf file in MAMP

This is a simple process, however not something I could find documented anywhere easy to find. MAMP supplies you with a range of sample configuration files:

/Applications/MAMP/Library/share/mysql/my-huge.cnf
/Applications/MAMP/Library/share/mysql/my-innodb-heavy-4G.cnf
/Applications/MAMP/Library/share/mysql/my-large.cnf
/Applications/MAMP/Library/share/mysql/my-medium.cnf
/Applications/MAMP/Library/share/mysql/my-small.cnf

To setup one of these for MAMP, simply issue the following command:

sudo cp /Applications/MAMP/Library/share/mysql/my-choicehere.cnf /Applications/MAMP/Library/my.cnf

How-To: Convert UIImage to Greyscale Equivalent

Here’s a simple code snippet for converting a UIImage to a greyscale equivalent:

-(UIImage *) convertToGreyscale:(UIImage *)i {
	
    int kRed = 1;
    int kGreen = 2;
    int kBlue = 4;
	
    int colors = kGreen;
    int m_width = i.size.width;
    int m_height = i.size.height;
	
    uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
	
    // now convert to grayscale
    uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
    for(int y = 0; y < m_height; y++) {
        for(int x = 0; x < m_width; x++) {
			uint32_t rgbPixel=rgbImage[y*m_width+x];
			uint32_t sum=0,count=0;
			if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
			if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
			if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
			m_imageData[y*m_width+x]=sum/count;
        }
    }
    free(rgbImage);
	
    // convert from a gray scale image back into a UIImage
    uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);
	
    // process the image back to rgb
    for(int i = 0; i < m_height * m_width; i++) {
        result[i*4]=0;
        int val=m_imageData[i];
        result[i*4+1]=val;
        result[i*4+2]=val;
        result[i*4+3]=val;
    }
	
    // create a UIImage
    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIImage *resultUIImage = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
	
    // make sure the data will be released by giving it to an autoreleased NSData
    [NSData dataWithBytesNoCopy:result length:m_width * m_height];
	
    return resultUIImage;
}

Thanks to the friendly people at StackOverflow.com for this snipet.

Google Maps: Zoom Level, Center from points on map

So you’ve created a pretty Google Map on your website.  You place a whole heap of markers on your map, driven from data you’ve got stored in your database.

A nice thing to do when we first show the map, would be to ensure that all the markers are visible on the map.

To do this, we need to set the zoom level and center point of the map dynamically. So how do we do this?  The answer is GLatLngBounds():

// Get your locations from the database
var mapLocations = <? echo json_encode(Locations::Find()) ?>;
	
// Initialise our map, add some controls and a default center point 
var map = new GMap2(document.getElementById("Mall_Map"));
map.setCenter(new GLatLng(15, 20), 2);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

// Now the fun part - create a GLatLngBounds object
	var bounds = new GLatLngBounds();

//
for (var i=0; i<mapLocations.length; i++)
{
        var location = mapLocations[i];
        var point = new GLatLng(location.Latitude, location.Longitude);

        // createMarker() is a custom function which returns a marker for a given GLatLng
        var marker = createMarker(point);
       
        // extend our bounds to include this point
        bounds.extend(point);

        // add our marker
	map.addOverlay(marker);
}

// Now dynamically set the center and zoom level for our map based on our bounds!
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 

The key is the use of the line:

map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 

This uses our bounds (which we’ve dynamically extended to include each of our points) to determine what the center and bounds zoome level should be.

How-To: Recursively remove .svn folders

Okay, so you’ve accidentally adding a bunch of files to SVN.  Or, you need to copy a bunch of files but you don’t want to take the .svn folders with you.  How to get rid of these?  On any *nix machine (Mac included) you can run the following command:

rm -rf `find . -type d -name .svn`

How-To: Convert NSData to NSString

How do you convert an NSData object into it’s string representation?  Should be easy, right?  It is, when you know how…

NSString* theString = [[NSString alloc] initWithData:theData encoding:NSASCIIStringEncoding];

iPhone UIScrollView with UIImageView have issues Interface Builder

Ever wanted to have an image view in a scrollview? Sure, you could directly add it using code, but wouldn’t it be nice to do it in Interface Builder?

If you’re like me, you like to leave on the defaults unless absolutely necessary. I had an issue where I added an image view to the scroll view in Interface Builder.
The image view then had an image added to in code. I resized the content view of the scroll view in code to be the same size as the image view (which you have to do
in order to get it to scroll).

Unfortunately, when an image larger than the scrollview was used, it went off the top of the scroll view by about 30px. I thought its origin was using the main view,
instead of the scroll view’s frame, but after modifying various properties I had no luck – and strange results, with the image always returning to about 30px higher than
the scroll view content area.

After trying to resolve things programattically, I resorted to Interface Builder again. I tried any number of properties on the scroll view to get its content
placed at its origin. It turns out that, by default, a UIImageView has a default “Mode” of center. That affects its alignment in its parent view. This ain’t
HTML kids, it is the elements themselves who decide how they are aligned in their parent elements. So, I just change the “Mode” to “Top Left”, and Robert’s your mother’s
brother.

Top Left Screenshot

The code for getting the scroll view to scroll (just FYI) is included below. If thisImage is bigger than the scroll view in any dimension, it will let you scroll
in the corresponding direction – but if it’s the same size or smaller, no scrolling is possible in that direction. Pretty nice. self.scrollView, by the way, has
only the default Interface Builder properties.

CGImageRef imageRef = thisImage.CGImage;
CGFloat width = CGImageGetWidth(imageRef);
CGFloat height = CGImageGetHeight(imageRef);

[self.scrollView setContentSize:CGSizeMake(width, height)];

How-To: Detect when MKAnnotation, MKAnnotationView is selected

I’m using MapKit to display a satellite map in one of my apps.  I create custom annotations – and it all works great.  However I wanted to be able to play sounds when a user touches one of my MKAnnotation’s on my MapKit MKMapView so that the sound matches the display of the callout (and when it disappears too).

There is no delegate method or other built-in mechanism for detecting when your annotation is selected, however it does have a selected property.  So how to detect when the MKAnnotation is selected and then play my sounds?  The answer is key/value observing.

Setup an observer on our imageAnnontationView:

// At the top of the .m file put:
static NSString* const GMAP_ANNOTATION_SELECTED = @"gMapAnnontationSelected";
// Then later somewhere in your code, add the observer
[imageAnnotationView addObserver:self
		 forKeyPath:@"selected"
		options:NSKeyValueObservingOptionNew
		context:GMAP_ANNOTATION_SELECTED];

Then we get a callback whenever the selected property of our annotation changes:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED]){
      BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
     // do something
   }
}

The value of annotationAppeared will change based on the state of the annontations selected property.  GMAP_ANOTATION_SELECTED is a constant string I set at the top of my file.

I’ve updated my post to make the GMAP_ANOTATION_SELECTED constant more obvious.  Hopefully this helps people use the snippet above.