Asynchronous Image Downloading Using NSOperation and NSInvocation in iPhone and iPad Development

downloading an image from url ,, here is pretty straight forward method which blocks UI , if it is huge. because this is synchronous method

NSData *imageData = [NSData dataWithContentsOfURL:yourURL];
UIImage *image = [UIImage imageWithData:imageData];

So here is method to download asynchronously

dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);

dispatch_async(downloadQueue, ^{
[NSData dataWithContentsOfURL:photoURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// Code to show the image in the UI goes here
});
});

this wont block your UI, hope it will help someone.

How to download image from url in iphone and iPad Development

Here is the code for getting image from url.

NSData *imageData = [NSData dataWithContentsOfURL:yourURL];
UIImage *image = [UIImage imageWithData:imageData];

replace url Link with your url address

Note: this is synchronous method,,
this method will freeze app if your image size is too big , in that case use asynchronous method

Sharing content through Twitter and facebook in ios

Almost Everyone in this world will expect that what others say about our app or our content,Being a mobile application developer how it will be if we integrate a feature to share the app content or features to the social media, would be nice right. Yes.

Here apple gives us a framework to make our work easier to spread our content around the world . Lets toss the coin to decide Twitter or Facebook gets their first place , oh ya Twitter on mark, Please read bit below , you can get the Twitter’s sample code followed by Facebook code snippet . Oh ya you got the code snippets  but i forgot saying the framework to be added to build it without compile error.

We have to add Social framework in your project then paste and modify the below codes in your class where you like spread the content of your app to globe

mySLComposerSheet = [[SLComposeViewController alloc] init];

mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

[mySLComposerSheet setInitialText:@””];

[mySLComposerSheet addImage:[UIImage imageNamed:@””]];

[self presentViewController:mySLComposerSheet animated:YES completion:nil];

For FaceBook:

mySLComposerSheet = [[SLComposeViewController alloc] init];

mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

[mySLComposerSheet setInitialText:@””];

[mySLComposerSheet addImage:[UIImage imageNamed:@””]];

[self presentViewController:mySLComposerSheet animated:YES completion:nil];

Please post your comments or share my page , if you like/love it

compose mail view controller with in application in iPhone and iPad development

iOS 3.2 and later apple provides a framework called  messageUI  framework to achieve Mail composing .

so first we need to add this framework in your project, Hope that we aware of adding a framework.
then just add these lines where you want in your app.

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@”My Subject”];
[controller setMessageBody:@”Hello there.” isHTML:NO];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];

and for call back implement this delegate method

(void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(@”It’s away!”);
}
[self dismissModalViewControllerAnimated:YES];
}

how to merge two UIImage and creating single Image

Sometimes we might need to merge or combine two Images and assign it to single UIImage Object ..the following code works exactly for that.

UIImage *firstImage = [UIImage imageNamed:@”a.png”];
UIImage *secondImage = [UIImage imageNamed:@”b.png”];

CGFloat firstWidth = CGGetImageWidth([firstImage CGImage]);
CGFloat firstHeight = CGGetImageHeight([firstImage CGImage]);

CGFloat secondWidth = CGGetImageWidth([secondImage CGImage]);
CGFloat secondHeight = CGGetImageHeight([secondImage CGImage]);

CGSize sizetoNewImage = CGSizeMake(firstWidth+secondWidth , firstHeight); // Here merging two images horizontally ,

UIGraphicsBeginImageContext(sizetoNewImage);

[firstImage drawInRect : CGRectMake(0,0,firstWidth,firstHeight)];
[secondImage drawInRect:CGRectMake(firstWidht,0,secondWidth,secondHeight)];// Here merging two images horizontally ,

UIImage *MergedImage  = UIGraphicsGetImageFromCurrentContext();

UIGraphicsEndImaecontext();

//Thats it

Expecting FeedBack from you

How to Assign your Game data (Textures) With box2D body

When you are using Box2D in Your Game Development you might need to set graphics to box2d , so here is the step to do this.

UserData is a void pointer so you can assign your Game Data Object (eg: image object)

//set up dynamic body
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
myBodyDef.position.Set(0, 20);
b2Body* body = world->CreateBody(&myBodyDef);

//set this Ball object in the body’s user data

body->SetUserData( this ); // here you can assign any object

//add circle fixture
b2CircleShape circleShape;
circleShape.m_p.Set(0, 0);
circleShape.m_radius = m_radius;
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &circleShape;
myFixtureDef.density = 1;
body->CreateFixture(&myFixtureDef);
Now Lets Get Back this data and draw it:

b2Body* b = m_world->GetBodyList();//get start of list
while ( b != NULL ) {

//obtain Ball pointer from user data
Ball* ball = static_cast<Ball*>( b->GetUserData() );
if ( ball != NULL ) {
ball->m_position = b->GetPosition();
ball->m_angle = b->GetAngle();
ball->m_linearVelocity = b->GetLinearVelocity();
}

//continue to next body
b = b->GetNext();
}

SOURCE; For Clear Explanation refer this Link : http://www.iforce2d.net/b2dtut/user-data

Creating Body and add it in World (BOX2D)

 

So we have covered the tutorial in last page about physics world and how we can create it, now its time for adding something in the world and play with it
// creating BodyDefinition

Here we create Dynamic Bodies.

b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
// create Shape Definition

b2PolygonShape polygonShape;
polygonShape.SetAsBox(1, 1); //a 2×2 rectangle

//fixture definition
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &polygonShape;
myFixtureDef.density = 1;

//create dynamic bodies
m_world->CreateBody(&myBodyDef)->CreateFixture(&myFixtureDef); /// Body created and added into world