Sunday, March 30, 2014

Creating rounded ImageView in iOS app development

Implement CirclularLayer will form a rounded image  view



#import <QuartzCore/QuartzCore.h>

@interface CirclularLayer : CALayer
-(id)initWithImage:(NSString*)image WithFrame:(CGRect)frame;


@end


//
//  CirclularLayer.m
//  ImageViewFrameSample
//
//  Created by Mayu on 3/31/14.
//  Copyright (c) 2014 NCS. All rights reserved.
//

#import "CirclularLayer.h"

@implementation CirclularLayer

-(id)initWithImage:(NSString*)image WithFrame:(CGRect)frame{
    
    self=[super init];
    if (self) {
        
        self.cornerRadius = 16.0;
     
        self.borderWidth=4.0;
        self.borderColor = [UIColor darkGrayColor].CGColor;
        
        float heightOfImageLayer  = (CGFloat)frame.size.height;
        float widthOfImageLayer  = (CGFloat)frame.size.width;

        heightOfImageLayer = floorf(heightOfImageLayer);
        widthOfImageLayer=floorf(widthOfImageLayer);
        self.cornerRadius = heightOfImageLayer/2.0f;
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, heightOfImageLayer, heightOfImageLayer);
        
        
        [CATransaction begin];
        [CATransaction setAnimationDuration:0];
        self.contents = (id)[self getImage:[UIImage imageNamed:image]].CGImage;
        [CATransaction commit];
        
        
    }
    
    return self;
}

-(UIImage*)getImage:(UIImage*)image{
    
    UIImage *finalImage = nil;
    UIGraphicsBeginImageContext(image.size);
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGAffineTransform trnsfrm = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(1.0, -1.0));
        trnsfrm = CGAffineTransformConcat(trnsfrm, CGAffineTransformMakeTranslation(0.0, image.size.height));
        CGContextConcatCTM(ctx, trnsfrm);
        CGContextBeginPath(ctx);
        CGContextAddEllipseInRect(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height));
        CGContextClip(ctx);
        CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, image.size.width, image.size.height), image.CGImage);
        finalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    
    return finalImage;
    
}




@end

Character oriented programing in iOS game developemnt

I have started doing gaming apps in iOS platform using cocos2d framework.But I found something fishy in the game architecture and I would like to share with you all.

So far we came across how object oriented programming bring simple meaning to sophisticated system.But Its not that simple any more when the system grows up in a large picture.So we need to create something powerful than a simple object,which I called as Character in gaming. 

A Character can be created by special features,behaviour to each other ,time controllability,collision controllability etc.

For example in the gaming scenario,game can be implemented by defining its story plot,where many game characters acts on a scene and so on.
So its highly preferable talk in terms of character to easy the development,reduce the program complexity,reduce the execution time and so on.

So In a complex program I suggest to look close what that application all about and talk in terms of its character.