2011年7月6日 星期三

checksum function

#import <CommonCrypto/CommonDigest.h>


- (NSString *)md5StringFromData:(NSData *)data
{
    void *cData = malloc([data length]);
    unsigned char resultCString[16];
    [data getBytes:cData length:[data length]];
    
    CC_MD5(cData, [data length], resultCString);
    free(cData);
    
    NSString *result = [NSString stringWithFormat:
                        @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                        resultCString[0], resultCString[1], resultCString[2], resultCString[3], 
                        resultCString[4], resultCString[5], resultCString[6], resultCString[7],
                        resultCString[8], resultCString[9], resultCString[10], resultCString[11],
                        resultCString[12], resultCString[13], resultCString[14], resultCString[15]
                        ];
    return result;
}

reference from :http://stackoverflow.com/questions/1028742/compute-a-checksum-on-the-iphone-from-nsdata

2011年6月26日 星期日

Detect the version of app

NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

2011年6月19日 星期日

get rgb data from UIImage

xxx.h

typedef unsigned char byte;

typedef struct RGBPixel    
{
    byte    red;
    byte    green;
    byte    blue;
}   RGBPixel;

typedef struct RGBAPixel    
{
    byte    red;
    byte    green;
    byte    blue;
    byte    alpha;    
}   RGBAPixel;

@interface UIImage(XXX)
- (RGBPixel*)bitmapFromBMP;
- (RGBAPixel*)bitmapFromPNG;
@end

xxx.m

#define RGBA                4
#define RGBA_8_BIT          8

@implementation UIImage(XXX)

- (RGBPixel*)bitmapFromBMP
{
    CFDataRef bitmapData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage)); 
    UInt8* pixelByteData = malloc(CFDataGetLength(bitmapData));
    if (!pixelByteData)
        return nil;
    
    CFDataGetBytes(bitmapData, CFRangeMake(0, CFDataGetLength(bitmapData)), pixelByteData);
    return (RGBPixel*)pixelByteData;
}

- (RGBAPixel*)bitmapFromPNG
{
    size_t bytesPerRow = self.size.width * RGBA;
    size_t byteCount = bytesPerRow * self.size.height;
    CGContextRef context = 0;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    UInt8* pixelByteData = malloc(byteCount);
    RGBAPixel* pixelData = nil;
    
    if (!colorSpace) 
    {
        return nil;
    }
    
    if (!pixelByteData)  
    {
        CGColorSpaceRelease( colorSpace );
        return nil;
    }
    context = CGBitmapContextCreate((void*) pixelByteData,
                                    self.size.width,
                                    self.size.height,
                                    RGBA_8_BIT,
                                    bytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaPremultipliedLast);
    
    if (!context)
    {
        free( pixelByteData );
        CGColorSpaceRelease( colorSpace );
        return nil;
    }
    CGRect rect = { { 0 , 0 }, { self.size.width, self.size.height }    };    
    CGContextDrawImage( context, rect, self.CGImage );
    pixelData = (RGBAPixel*) CGBitmapContextGetData(context);
    
    return pixelData;
    
}

@end

在 iPhone 上實現小畫家基本功能

.h

UIImageView *drawImage_;
CGPoint lastPoint_;

.m
-(id) init
{
if( (self=[super init])) {
        drawImage_ = [[[UIImageView alloc]initWithImage:nil]autorelease];
        drawImage_.frame = CGRectMake(0, 50, 320,400);
        [[[CCDirector sharedDirector]openGLView] addSubview:drawImage_];
        self.isTouchEnabled = YES;
}
return self;
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    lastPoint_ = [touch locationInView:drawImage_];    
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:drawImage_];
    UIGraphicsBeginImageContext(drawImage_.frame.size);
    
    [drawImage_.image drawInRect:CGRectMake(0, 0, drawImage_.frame.size.width, drawImage_.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0,0.0,0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint_.x, lastPoint_.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage_.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint_ = currentPoint;
}




2011年6月17日 星期五

Detect the orientation of iPhone view

step 1: add a notification

    [[NSNotificationCenter defaultCenter] addObserver:self 
                         selector:@selector(receivedRotate:)                                          
                             name:UIDeviceOrientationDidChangeNotification 
                           object:nil];



step 2: remove a notification

    [[NSNotificationCenter defaultCenter] removeObserver:self  
                             name:UIDeviceOrientationDidChangeNotification 
                           object:nil];


step 3: implement "receiveRotate"

- (void)receivedRotate:(NSNotification*)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (deviceOrientation == UIDeviceOrientationPortrait)
    {
        ...
    }
}

Transition between portrait and landscape

Portrait View :
WhatsOnLandscapeController *whatsOnLandscape = [[[WhatsOnLandscapeController alloc] initWithNibName:@"WhatsOnLandscape" bundle:nil]autorelease];

[whatsOnLandscape setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:whatsOnLandscape animated:YES];

Landscape View :
[Leave Landscape View]
[self dismissModalViewControllerAnimated:YES];

2011年6月15日 星期三

List iPhone Country Code

NSLocale *currentLocale = [NSLocale currentLocale];

NSArray *countryArray = [NSLocale ISOCountryCodes];

for (NSString *key in countryArray)
{
NSLog(@"code : %@\n",key);
NSString *displayNameString = [currentLocale displayNameForKey:NSLocaleCountryCode value:key];
NSLog(@"displayNameString : %@\n",displayNameString);
}


NSLog(@"Country Code is %@", [currentLocale objectForKey:NSLocaleCountryCode]);