Принимая вопрос Эрика и поблагодарив его, я предоставляю рабочий код для его предложенного решения.
Может, кому-то это поможет:
if (yAxisIcons) {
int custonLabelsCount = [self.yAxisIcons count];
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:custonLabelsCount];
for (NSUInteger i = 0; i < custonLabelsCount; i++) {
NSNumber *tickLocation = [NSNumber numberWithInt:i];
NSString *file = [yAxisIcons objectAtIndex:i];
UIImage *icon = [UIImage imageNamed:file];
CPImageLayer *layer; // My custom CPLayer subclass - see code below
CGFloat nativeHeight = 1;
CGFloat nativeWidth = 1;
if (icon) {
layer = [[CPImageLayer alloc] initWithImage:icon];
nativeWidth = 20;//CGImageGetWidth(icon.CGImage);
nativeHeight = 20;//CGImageGetHeight(icon.CGImage);
//layer.contents = (id)icon.CGImage;
if (nativeWidth > biggestCustomIconWidth) {
biggestCustomIconWidth = nativeWidth;
}
}else{
layer = [[CPImageLayer alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
CGRect startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);
layer.frame = startFrame;
layer.backgroundColor = [UIColor clearColor].CGColor;
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithContentLayer:layer];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
[customLabels addObject:newLabel];
[newLabel release];
[layer release];
}
y.axisLabels = [NSSet setWithArray:customLabels];
}
CPImageLayer.h
#import "CPLayer.h"
@interface CPImageLayer : CPLayer {
UIImage *_image;
}
-(id)initWithImage:(UIImage *)image;
@end
CPImageLayer.m
#import "CPImageLayer.h"
#import "CPLayer.h"
@implementation CPImageLayer
-(void)dealloc{
[_image release];
[super dealloc];
}
-(id)initWithImage:(UIImage *)image{
CGRect f = CGRectMake(0, 0, image.size.width, image.size.height);
if (self = [super initWithFrame:f]) {
_image = [image retain];
}
return self;
}
-(void)drawInContext:(CGContextRef)ctx{
CGContextDrawImage(ctx, self.bounds, _image.CGImage);
}
@end
Наслаждайтесь