views:

537

answers:

1

I am trying to use a separate delegate class (as many examples do) to perform the drawing of a large image in a CATiledlayer. The tiled layer has been added as a sub layer in a separate view. I first start out with a view controller.

//  ImageInTiledLayerViewController.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "MyView.h"
#import "MyDelegate.h"

@interface ImageInTiledLayerViewController : UIViewController {
    MyView *myView;
    MyDelegate *myDelegate;
    CATiledLayer *tiledLayer;
}

@property (nonatomic, retain) MyView *myView;
@property (nonatomic, retain) MyDelegate *myDelegate;
@property (nonatomic, retain) CATiledLayer *tiledLayer;

@end

//  ImageInTiledLayerViewController.m

#import "ImageInTiledLayerViewController.h"

@implementation ImageInTiledLayerViewController

@synthesize myView;
@synthesize myDelegate;
@synthesize tiledLayer;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    // create the view
    myView = [[MyView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1200.0, 800.0)];
    myView.backgroundColor = [UIColor whiteColor];

    // delegate for the titled layer
    myDelegate = [[MyDelegate alloc] init];

    // create the tiled layer
    tiledLayer = [CATiledLayer layer];
    tiledLayer.frame = CGRectMake(0.0, 0.0, 1200.0, 800.0);

    // set the tiled layers delegate for drawing
    tiledLayer.delegate = myDelegate;

    // add the tiled layer to the view
    [myView.layer addSublayer:tiledLayer];

    // add the view to this controllers view
    [self.view addSubview:myView];

    // tile layer needs layout
    [tiledLayer setNeedsLayout];
}

- (void)dealloc {
    [super dealloc];
}

@end

My view is just a basic view at the moment. I want this in a view because i will add it to scroll view later and it will respond to touch events.

//  MyView.h

#import <UIKit/UIKit.h>


@interface MyView : UIView {

}

@end

//  MyView.m

#import "MyView.h"


@implementation MyView


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
    [super dealloc];
}


@end

Here's where it all goes wrong...

//  MyDelegate.h

#import <Foundation/Foundation.h>


@interface MyDelegate : NSObject {
    CGImageRef myImageRef;
}

@property (nonatomic) CGImageRef myImageRef;

@end


//  MyDelegate.m

#import "MyDelegate.h"


@implementation MyDelegate

@synthesize myImageRef;

- (id)init {
    // assign the image reference
    myImageRef = [UIImage imageNamed:@"Waterfall.png"].CGImage; 
    return self;
}
// THIS NEVER GETS CALLED
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context {
    // re-orientate the context
    CGContextTranslateCTM(context, 0.0, CGImageGetHeight(myImageRef));
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, CGImageGetWidth(myImageRef), CGImageGetHeight(myImageRef)), myImageRef);
}

@end

Despite my call to setNeedsLayout for the tile layer the drawLayer method is never called and hence the image is never drawn.

A: 

You also miss a call to super in your delegate's init method:

if (self == [super init]) {
    // ...
}
Laurent Etiemble