Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Wednesday, September 1, 2010

iOS - Adding a stretchable UIImage as background


Often i need to use some image as a background for many visual components, like views, scrollviews, buttons, other images… and most of time i ended using a large enough image to cover the front image.
So, if my view was 1000x700 i needed to load an 1000x700 image for the background. But this image will eat a lot of memory just to be there as a background. There should be a better way to do this. And there is a better way to do this.
All you have to do is create your UIImage like this
UIImage* backgroundImage = [[UIImage imageNamed:@”stretchableImage.png”] stretchableImageWithLeftCapWidth:44 topCapHeight:45];
this stretchableImageWithLeftCapWidth specify how many pixel in the left must be kept. The same goes to the topCapHeight, and in this case it specifies the number of pixels in the top.
So in the example, the sdk will keep the first 44 pixels, copy the 45th along all the way of the image, and in the end keep again the last 44 pixels. It will do the same for the height.
So you wont need to keep a large image in memory. And you can also use this technique to create dynamic buttons, views and anything that you may even not know how length it is.

Here you can find an image as example (but keep in mind that as a designer, i’m a good programmer ^_^” )



Visit my tumblr for more iphone tips and tricks

Tuesday, August 24, 2010

How to load your data correctly in the background - iOS


Almost all view controllers need to load some data from the internet and interact with them.
This kind of task may sound simple at first, but as you will find out, there are some kind of complications, like autoreleasing objects with no autorelease pool in place, how to get notified when your data has been loaded and how to handle this data. Also all the updates to the view should be done in the main thread, not the background thread, to avoid possibly app crash.
To solve all these problems I’ve created a subclass of UiViewController that controls all the steps required to load the data the right way, and updates the view the right way.
Continue reading about this in my tumblr blog here

Sunday, January 31, 2010

How to make a POST request with iPhone sdk

To make a web request with the iPhone is very simple. The following code is an example of how you can do it. If you need to pass some variables to the server, just use the lines that are in bold.

NSURL *url = [NSURL URLWithString:@"http://www.google.com.br"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *requestBody = [@"username:x&password:y" dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBody];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];

 Ready. The variable responseString will have all the text that the server has returned.