绘制图像的几种写法

- (void)drawIrregular {
    UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, [UIScreen mainScreen].scale);
    // 绘制一个多边形 填充渐变色
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(10, 10)];
    [path addLineToPoint:CGPointMake(290, 10)];
    [path addLineToPoint:CGPointMake(290, 144)];
    [path addLineToPoint:CGPointMake(10, 194)];
    [path addLineToPoint:CGPointMake(10, 10)];
    // 渐变图层
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.imageView.bounds;
    gradient.colors = @[(__bridge id)[UIColor blueColor].CGColor,(__bridge id)[UIColor greenColor].CGColor];
    // x轴渐变
    gradient.startPoint = CGPointMake(0, 1);
    gradient.endPoint = CGPointMake(1, 1);
    [self.imageView.layer addSublayer:gradient];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    gradient.mask = shapeLayer;
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageView.image = img;
}
- (void)drawCoreGraphic {
    // 创建颜色空间
    CGColorSpaceRef  spaceRef = CGColorSpaceCreateDeviceRGB();
    float width = self.imageView.bounds.size.width;
    float height = self.imageView.bounds.size.height;
    int bitsPerCompent  = 8;
    int bytesPerRow = 4 * 8 * bitsPerCompent * width;
    // 上下文
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, bitsPerCompent, bytesPerRow, spaceRef, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
    // 绘制背景
    CGContextFillRect(context, self.imageView.bounds);
    CGContextSetRGBFillColor(context, 0.5, 0.22, 0.33, 1);
    CGContextSetRGBStrokeColor(context, 0, 1, 0, 1);
    CGContextFillRect(context, CGRectMake(0, 0, 300, 300));
    CGContextStrokeRect(context, CGRectMake(0, 0, width, height));
    
    UIImage *img=[UIImage imageNamed:@"1.png"];
    CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), img.CGImage);
    
    //  从context中获取CGImage, 并创建UIImage
    CGImageRef cgimg = CGBitmapContextCreateImage(context);
    UIImage *resultImg = [UIImage imageWithCGImage:cgimg];
    
    //  清理CoreGraphic 资源
    CGContextRelease(context);
    CGColorSpaceRelease(spaceRef);
    CGImageRelease(cgimg);
    
    self.imageView.image = resultImg;
}
-(void)draw2 {
    // 1. 创建 context, 并且将context push到UIKit维护的Context Stack
    UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, [UIScreen mainScreen].scale);
    // 2. 创建Path, 绘制背景和填充颜色
    UIBezierPath *rect = [UIBezierPath bezierPathWithRect:self.imageView.bounds];
    [[UIColor redColor] setFill]; // 作用于Context Stack 栈顶stack
    [rect fill];
    [[UIColor greenColor] setStroke]; // 作用于Context Stack 栈顶
    [rect stroke];
    
    // 3. 使用 UIKit 的drawing method,因此需要坐标系是 ULO, 这里使用 UIKit方法创建, 因此绘制的文字都是正的. 坐标原点是ULO
    NSString *text= @"文字";
    UIFont *font=[UIFont systemFontOfSize:24];
    [text drawAtPoint:CGPointMake(100, 100) withAttributes:font.fontDescriptor.fontAttributes];
    
    // 4. 用UIKit 绘制图像, context是UIKit创建的, 因此是ULO坐标系, (0,0,100,100)在左上角,并且图片是正向
    UIImage *img=[UIImage imageNamed:@"1.png"];
    [img drawInRect:CGRectMake(0, 0, 100, 100)];
    
    // 5. 从当前context获取UIImage
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageView.image = resultImg;
}
- (void)draw3 {
    // 创建颜色空间
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    float width = self.imageView.frame.size.width;
    float height = self.imageView.frame.size.height;
    int   bitsPerCompent = 8;
    int   bytesPerRow = 4*8*bitsPerCompent*width;
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, bitsPerCompent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
    UIGraphicsPushContext(context);
    // 创建背景颜色
//    CGContextFillRect(context, self.imageView.bounds);
    CGContextSetRGBFillColor(context, 110/255.0, 10/255.0, 220/255.0, 1.0);
    CGContextFillRect(context, self.imageView.bounds);
    
    // 修正坐标系
    CGContextTranslateCTM(context, 0, height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    UIImage *img = [UIImage imageNamed:@"1.png"];
//    CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), img.CGImage);
    [img drawInRect:CGRectMake(0, 0, 100, 100)];

    CGImageRef cgimg = CGBitmapContextCreateImage(context);
    UIImage *resultImg = [UIImage imageWithCGImage:cgimg];
    
    UIGraphicsPopContext();
    // 释放资源
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(cgimg);
    self.imageView.image = resultImg;
}

你可能感兴趣的:(绘制图像的几种写法)