Jlennon321
Jlennon321

Reputation: 228

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
{
    if(_map!=nil){
        UIGraphicsBeginImageContext(rect.size);
        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)];
            }
        }
        UIGraphicsEndImageContext();
    }
}

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

Any help would be greatly appreciated. Thanks!

Upvotes: 0

Views: 1599

Answers (1)

yurish
yurish

Reputation: 1535

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.

Upvotes: 3

Related Questions