通过 CALayer 代理方法绘图

#import "ViewController.h"
#define WIDTH 50
#define PHOTO_HEIGHT 150
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    CALayer *layer=[CALayer layer];
    layer.bounds=CGRectMake(0, 0, PHOTO_HEIGHT, PHOTO_HEIGHT);
    layer.position=CGPointMake(160, 200);
    layer.backgroundColor=[UIColor redColor].CGColor;
    layer.cornerRadius=PHOTO_HEIGHT/2;
    layer.masksToBounds=YES;
    
    layer.borderColor=[UIColor whiteColor].CGColor;
    layer.borderWidth=2;
    layer.delegate=self;
    [self.view.layer addSublayer:layer];
    [layer setNeedsDisplay];
    
    
    
    
}
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGContextSaveGState(ctx);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -PHOTO_HEIGHT);
    UIImage *image=[UIImage imageNamed:@"1"];
    CGContextDrawImage(ctx, CGRectMake(0, 0, PHOTO_HEIGHT, PHOTO_HEIGHT), image.CGImage);
    CGContextRestoreGState(ctx);
}

posted @ 2015-03-12 14:43  曹县三胖暴打大猩猩  阅读(174)  评论(0编辑  收藏  举报