1.UIColor生成图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:imageView];
UIColor *color = [UIColor redColor];
UIGraphicsImageRenderer *re = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(100, 100)];
UIImage *image = [re imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[color setFill];
[rendererContext fillRect:CGRectMake(0, 0, 100, 100)];
}];
imageView.image = image;
2.layer生成图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:imageView];
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = CGRectMake(0, 0, 100, 100);
layer.locations = @[@(0.0), @(1.0)];
layer.startPoint = CGPointMake(0, 0);
layer.endPoint = CGPointMake(1, 0);
layer.colors = @[(__bridge id)HexRGB(0x2ACAE6).CGColor,
(__bridge id)HexRGB(0x17D1A5).CGColor];
UIGraphicsImageRenderer *re1 = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(100, 100)];
UIImage *image1 = [re1 imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[layer renderInContext:rendererContext.CGContext];
}];
imageView.image = image1;
3.绘制文本
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:imageView];
CGSize size = CGSizeMake(100, 100);
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = 0;
format.opaque = NO;
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format];
UIImage *markImage = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[[UIColor blackColor] set];
[@"123123123" drawAtPoint:CGPointZero
withAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:19 weight:(UIFontWeightMedium)],
NSForegroundColorAttributeName: [UIColor blackColor]}];
}];
imageView.image = markImage;
4.图片绘制圆角
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[self.view addSubview:imageView];
UIImage *image2 = [UIImage imageNamed:@"change.jpeg"];
UIGraphicsImageRendererFormat *format1 = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = [UIScreen mainScreen].scale;
format.opaque = NO;
UIGraphicsImageRenderer *render1 = [[UIGraphicsImageRenderer alloc] initWithSize:image2.size format:format1];
UIImage *circleImage = [render1 imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
CGRect rect = CGRectMake(0, 0, image2.size.width, image2.size.height);
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, rect);
CGContextClosePath(context);
CGContextClip(context);
[image2 drawInRect:rect];
}];
imageView.image = circleImage;
5.UIColor生成图片并切圆角
- (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size radius:(CGFloat)radius {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = [UIScreen mainScreen].scale;
format.opaque = NO;
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format];
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
CGContextSetFillColorWithColor(context, color.CGColor);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];
CGContextAddPath(context, path.CGPath);
CGContextFillPath(context);
}];
return image;
}