UIImage drawinrect method doesn't draw image

I have created a cust UIView class so that I can display the contents of a Map object. After much research, I am still unable to determine why the method below doesn't draw anything:

- (void)drawRect:(CGRect)rect
        UIImage *img = [UIImage imageNamed:@"grass"];
        float w =  rect.size.width / _map.width;
        float h =  rect.size.height / _map.height;
        for(int x=0; x<_map.width; x++){
            for(int y=0; y<_map.height; y++){
                Tile *tile = [_map getTileAtX:x Y:y];
                UIImage *img = [UIImage imageNamed:NSStringFromTileType(tile.type)];
                [img drawInRect:CGRectMake(x*w, y*h, w, h)];

I am calling [mapView setNeedsDisplay] to refresh the UIView.

Any help would be greatly appreciated. Thanks!


In your code UIGraphicsBeginImageContext(...) pushes new image context on top of the context of the view and drawInRect: draws the image into this new context. UIGraphicsEndImageContext() pops the image context destoying its contents. Pushing image context is usually used when you want to create UIImage, not when you need to draw its content into a context.

If you want to see the image - delete UIGraphicsBeginImageContext and UIGraphicsEndImageContext calls.

Need Your Help

JAXB Marshaller initialization

java xml xml-serialization jaxb marshalling

I need to Marshal a JAXB object to xml format string. I'm using a SLSB and hook the code to create the Marshaller and other things in @PostConstruct annotated method. So that each time I need not l...

How to freeze a column in Grid and make it always shown in client area of a WPF user control

c# wpf grid

I searched in stackoverflow, and there is a property FrozenColumnCount for DataGrid, but Grid control doesn't have that property.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.