I wrote the following minimalistic iPhone app for drawing a simple png file onto a CALayer object.
Here is the code
And the implementation
I checked with the UIImageView that the png file is correctly found and it is displayed in the UIImageView. But if I run the code as above nothing will be drawn to the CALayer noteLayer. Just a blue square with no content.
I ran the program in the debugger and set a breakpoint to have look at the CGImageRef object img. When hovering with the mouse over the obeject the debugger tells me a memory address so the object doesn't seem to be nil. But the output of NSLog is strange, it displays the following output
I do not understand what is wrong in my very simple program???
Here is the code
Code:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface DrawPNGOnCALayerViewController : UIViewController {
CGImageRef img;
CALayer* noteLayer;
UIImageView* imgView;
}
@end
And the implementation
Code:
#import "DrawPNGOnCALayerViewController.h"
@implementation DrawPNGOnCALayerViewController
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
noteLayer = [[CALayer alloc] init];
[self.view.layer addSublayer:noteLayer];
/*imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"note.png"]];
[self.view addSubview:imgView];*/
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {}
- (void)viewWillAppear:(BOOL)animated {
/*
noteLayer.delegate = self;
noteLayer.bounds = CGRectMake(0.f, 0.f, 200.f, 200.f);
noteLayer.backgroundColor = [[UIColor blueColor] CGColor];
noteLayer.position = self.view.center;
[noteLayer setNeedsDisplay];
*/
UIImage* image = [UIImage imageNamed:@"note.png"];
img = CGImageRetain( image.CGImage );
noteLayer.delegate = self;
noteLayer.bounds = CGRectMake( 0.f,0.f, 200., 200. );
noteLayer.backgroundColor = [[UIColor blueColor] CGColor];
NSLog(@"img = %f,%f",CGImageGetWidth(img),CGImageGetHeight(img));
noteLayer.contents = (id)img;
noteLayer.position = self.view.center;
[noteLayer setNeedsDisplay];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
I checked with the UIImageView that the png file is correctly found and it is displayed in the UIImageView. But if I run the code as above nothing will be drawn to the CALayer noteLayer. Just a blue square with no content.
I ran the program in the debugger and set a breakpoint to have look at the CGImageRef object img. When hovering with the mouse over the obeject the debugger tells me a memory address so the object doesn't seem to be nil. But the output of NSLog is strange, it displays the following output
2010-10-21 16:10:36.963 DrawPNGOnCALayer[72288:207] img = 0.000000,13510798882111488.000000
I do not understand what is wrong in my very simple program???