我正在尝试使用单独的委托类(尽可能多的示例)来在CATiledlayer中执行大图像的绘制。平铺图层已作为子图层添加到单独的视图中。我首先从一个视图控制器开始。
// 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
我的观点目前只是一个基本观点。我希望在视图中使用它,因为我稍后会将其添加到滚动视图中,它将响应触摸事件。
// 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
这就是出错的地方......
// 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
尽管我对tile图层调用了setNeedsLayout,但是从不调用drawLayer方法,因此永远不会绘制图像。
答案 0 :(得分:0)
你也错过了委托的init方法中对super的调用:
if (self = [super init]) {
// ...
}
答案 1 :(得分:0)
你正在重新绘制CATiledLayer而是使用它,你应该是golen。
-(void)drawLayer:(CATiledLayer*)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);
}