Pages

Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, July 18, 2009

iPhone OS - drawing image and stupid thing from Apple

Just because of a piece of code, a team in Singapore gave up an iPhone project. Actually, they do not know how to play tricks with Apple. ... and I also faced to a similar situation when I play as a technical consultant to fix this stupid bug.

The need of client is: drawing some effects on an image and save it as a photo in Photo library. So, if anyone has experience with graphic programming may think this is a very simple task. However, that's not true.

Many forums and online articles just shows us a real problem on iPhone graphics programming: the root position (0,0) of the image to draw is on the bottom left and the root position of the graphic context is on the top-left corner. So, we must do some transformation to keep the image in the correct position.

And the simple code to fix this issue is as below:
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
//create the rect zone that we draw from the image
CGRect imageRect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);
//Do stupid stuff to draw the image correctly
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, imageRect, image.CGImage);
//After drawing the image, roll back all transformation by restoring the 
//old context
CGContextRestoreGState(context);
DO OTHER EFFECTS HERE
//get the image from the graphic context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//commit all drawing effects
UIGraphicsEndImageContext();
Above piece of code is thing that you see in all online technical articles related to iPhone graphic programming. Anyway, that's not enough. If you deploy that piece of code into iPhone OS 3.0 on real device, it does not run correctly: saved images are always scaled in horizontal and inverse. However, there is a very strange thing: this code is run perfectly on simulator for iPhone OS 3.0.

After 1 day to detect the problem, I found that: when an image is shown on iPhone, it has a direction. Direction is included: UP, DOWN, LEFT, RIGHT. So, we must to fix the code to satisfy when they're in those cases. The above code is run correctly in case image direction is UP.

Here is the fixed version:
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
//create the rect zone that we draw from the image
CGRect imageRect;

if(image.imageOrientation==UIImageOrientationUp 
|| image.imageOrientation==UIImageOrientationDown) 
{
    imageRect = CGRectMake(0, 0, width, height); 
}
else 
{
    imageRect = CGRectMake(0, 0, height, width); 
}

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);

//Do stupid stuff to draw the image correctly
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);

if(image.imageOrientation==UIImageOrientationLeft) 
{
    CGContextRotateCTM(context, M_PI / 2);
    CGContextTranslateCTM(context, 0, -width);
}
else if(image.imageOrientation==UIImageOrientationRight) 
{
    CGContextRotateCTM(context, - M_PI / 2);
    CGContextTranslateCTM(context, -height, 0);
} 
else if(image.imageOrientation==UIImageOrientationUp) 
{

//DO NOTHING

}
else if(image.imageOrientation==UIImageOrientationDown) 
{
    CGContextTranslateCTM(context, width, height);
    CGContextRotateCTM(context, M_PI);
}

CGContextDrawImage(context, imageRect, image.CGImage);
//After drawing the image, roll back all transformation by restoring the 
//old context
CGContextRestoreGState(context);
DO OTHER EFFECTS HERE
//get the image from the graphic context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//commit all drawing effects
UIGraphicsEndImageContext();
After fixing this bug, I can say only one thing: why Apple always do something stupid and make developer do the stupid thing?

Saturday, June 13, 2009

iPhone programming kungfu

I have a bad habit: before starting learning, researching something, I always want to know what is the highest level of the expert in that domain. Usually I map the research domain into kungfu. I call this bad habit because it often takes my time to know that.
In recent months, we setup a team to research iPhone programming. So, this is a good chance for me to learn a new kungfu. After a few months, I identified the Kungfu levels of iPhone programming. It helps me to know what our current level is to improve more skill.

iPhone programming kungfu

Level 1: Tool master

At this level, you must know:
+ Tools using in iPhone programming, the role and how to use them.
+ MAC OS environment: if you're user from Window, Unix, ... you will be surprise with differences on MAC OS and you must get acquainted with the environment here.
+ Hack to install MAC OS in PC: you should know this if you do not have money to buy a real MAC.
+ Master of languages using in iPhone programming: Objective C, C

Level 2: Native application master

At this level, you must have strong experience with building user interface for native application. Below is the list of thing required:
+ Have deep understanding about Model View Controller pattern and how to implement that using iPhone SDK.
+ Strong knowledge about common UIControl.
+ Experience with touch programming.
+ Must know user interface patterns in iPhone interface design.
+ Familiar with data storage: XML, SQLLite
+ Familiar with interact with calling remote services.
+ Familiar with UI animation and effects.

Level 3: Game programming master

Game is a common application type on iPhone. So, you should know how to develop a game on iPhone. You can use some popular game engines on iPhone to do that. Currently, we choose Cocos2D for the 2D game engine and Chipmunk as the physic engine behind. (Remember that: a game engine can not be completed if it does not have a physic engine)
I don't want to talk much about this level because I am not a master in this domain (game programming). However, below is the required list for level 3 (that I've discovered until now)
+ Can load/use sprite/bitmap into game engine
+ Create scene and navigate between scenes
+ Move and animate with 2D object
+ Using physic engine to create physic effect and animation.

Note: the above list is just for 2D game programming. You can find another list in 3D programming - but it is out of the scope because we do not have enough land to write about that here.

Level 4: Web programming master

At this level, you must know:
+ HTML tags that allows in iPhone safari browser.
+ Layout an HTML page to view best on iPhone
+ Using iUI library to build a web app liked native app.

Level 5: iPhone kungfu master

This is the highest level in iPhone programming. At this level, you can understand and program fluently with 4 above levels. Besides that, you must know:
+ Using advanced services and utilities in iPhone: geopositioning (GPS), maps, weather.
+ Can combine both Web + Native application into one.
+ Can design the system architecture for bigger application on iPhone with big storage data, synchronization solution, ...
+ Know how to optimize the program on iPhone and how to use tool for detect leak memory and high rate CPU cost in code.
Above is the list of 5 levels - kungfu in iPhone programming. Please feel free to post your comments or another level if you found.
Happy programming!

Saturday, November 8, 2008

Object relation mapping - Is it a Vietnam war science?

the Vietnam war


I read this article 1,2 years ago. I think it's valuable for anyone loves Object Relation mapping. You should read it carefully and think about what you applied with ORM in your projects.
The original article is: The Vietnam of Computer Science


You can find some more interesting related articles:



+ Object-Relational Mapping is the Vietnam of Computer Science
+ Thoughts on Vietnam commentary
Actually, I am a fan of ORM. I've ever had nightmare with Hibernate (a famous ORM framework). It takes my team many weeks to optimize the performance of the whole system. However, it does not break my dream about it.
If I have free time, I will post an entry to share with you some of my experiences in Hibernate (a famous ORM framework). Wait'n see.
Reblog this post [with Zemanta]

Tuesday, November 4, 2008

Javascript Object Oriented Programming

Javascript is a prototype based language (not a class based language). You can read more here to understand differences between prototype based and class based language.

However, prototype has some good features that help you can implement the Object oriented programming model. Nowadays, a lot of javascript frameworks support us can implement OOP easily (like Prototype, JQuery, …). In this post, I do not want to introduce about them. I would like to help you understand about Javascript OOP without any support from other external libraries or frameworks.

Index

1. Defining a class

2. Common definitions and implementation

3. Inheritance in Javascript

1. Defining a class

To define a class in Javascript, we can use one of below ways

1. Traditional way

function className ([params] ) {

//properties and functions

}

2. Second way

className = function( [params]) {

//properties and function

}

2. Common definitions and implementation

  • private variables are declared with the ‘var’ keyword inside the object, and can only be accessed by private functions and privileged methods.Example
    function Person {
    var name = “KBird”;
    }
  • private functions are declared inline inside the object’s constructor (or alternatively may be defined via var functionName=function(){}) and may only be called by privileged methods (including the object’s constructor).Example
    function Person {
    this.name = “KBird”;
    var getName = function()
    {
    return this.name;
    }
    };
  • privileged methods:privileged methods are declared with
    this.methodName=function
    (){}
    and may invoked by code external to the object.
  • public properties are declared with this.variableName and may be read/written from outside the object.
  • public methods are defined by Classname.prototype.methodName = function(){} and may be called from outside the object.
  • prototype properties are defined by Classname.prototype.propertyName = someValue
  • static properties are defined by Classname.propertyName = someValue

3. Inheritance in Javascript

Inherit from a super class

subclassName.prototype = new subClass;

Override a function

subclassName.prototype.functionNeedToOverride =function(params) {

}

To be continue

Reblog this post [with Zemanta]

Javascript history

In this post, I just collect information from Wikipedia and some portfolios of persons who makes Javascript to be success today.
Everything has its history. Understand the history of Javascript helps you have a broader knowledge and makes you more interesting in working with this great programming language.

JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, which was later renamed to LiveScript, and finally to JavaScript. The change of name from LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. JavaScript was first introduced and deployed in the Netscape browser version 2.0B3 in December 1995. The naming has caused confusion, giving the impression that the language is a spin-off of Java, and it has been characterized by many as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new web-programming language.

Microsoft named its dialect of the language JScript to avoid trademark issues. JScript was first supported in Internet Explorer version 3.0, released in August 1996, and it included Y2K-compliant date functions, unlike those based on java.util.Date in JavaScript at the time. The dialects are perceived to be so similar that the terms “JavaScript” and “JScript” are often used interchangeably (including in this article). Microsoft, however, notes dozens of ways in which JScript is not ECMA compliant.

Netscape submitted JavaScript to Ecma International for standardization resulting in the standardized version named ECMAScript.

(From wikipedia: http://en.wikipedia.org/wiki/JavaScript)

Who make Javascript success?

Brendan Eich: the man invented Javascript


Brendan Eich

he created Javascript when he was in Netscape. Currently, Brendan Eich is CTO of Mozilla Corporation



...



Douglas Crockford (JSON)

Discover Javascript and JSON.

He is well known for his work in introducing JavaScript Object Notation (JSON).

You can enjoy a lot of his good articles here

http://crockford.com/

http://blog.360.yahoo.com/douglascrockford





John Resig (JQuery)

pushed Javascript forward. He is the man creates JQuery - this is one of biggest successful projects of Javascript.

Now, John Resig is a JavaScript Evangelist for the Mozilla Corporation and the author of the book Pro JavaScript Techniques.




Sam Stephenson (PROTOTYPE)

Sam Stephenson

Sam created the Prototype JavaScript framework in February of 2005 as part of the foundation for Ajax support in Ruby on Rails. He lives in Chicago, writes web applications for 37signals, and tumblelogs on Projectionist.


Javascript programming

While working with a lot of web development teams, I found a truth: most of developers (at least: Vietnamese developers) do not think Javascript is important. Most of them use javascript as tips and tricks - just google and apply every script they found to solve their problem. Parts of them try to prevent using javascript with complex technique into their project - with them Javascript is a nightmare.

I agree that writing Javascript to make the code run cross browser is complex and difficult with some developer. However, that’s not a big problem if you have a deep knowlege about Javascript. Everything has a little bit difficult starting. Right?

In this series of posts, I don’t want to dig into old knowledge about syntax, basic types, … in Javascript. If you want to know about that, you can get them with google. This series of post only focuses into these points:

1. Javascript introduction

2. Object oriented programming in Javascript

3. Unit testing with Javascript

4. Introduce some Javascript frameworks nowadays.

I am writing these parts and will post them one by one.

Wait ‘n enjoy.

Reblog this post [with Zemanta]