一、CAGradientLayer概述
@interface CAGradientLayer : CALayer
@property(nullable, copy) NSArray *colors;
@property(nullable, copy) NSArray<NSNumber *> *locations;
@property CGPoint startPoint;
@property CGPoint endPoint;
@property(copy) NSString *type;
@end
/** `type' values. **/
CA_EXTERN NSString * const kCAGradientLayerAxial
二、制作渐变的view
- (UIView *)makeGradientViewWithColors:(NSArray *)colorsArray {
UIView *viewGreadient = [[UIView alloc]initWithFrame:(CGRect){100,300,200,100}];
CAGradientLayer *layer = [[CAGradientLayer alloc]init];
layer.frame = viewGreadient.bounds;
[viewGreadient.layer addSublayer:layer];
NSArray *arrayColors = colorsArray;
NSMutableArray *arrayCGColor = [NSMutableArray array];
NSEnumerator *enu = [arrayColors objectEnumerator];
UIColor *color = nil;
while (color = [enu nextObject]) {
[arrayCGColor addObject:(__bridge id)color.CGColor];
}
layer.colors = arrayCGColor;
layer.startPoint = CGPointMake(0.5, 0);
layer.endPoint = CGPointMake(0.5, 1);
return viewGreadient;
}
三、图片的合成、重绘
/** 将一个Imag制作成图片 */
- (UIImage *)redrawImage:(UIImage *)Image {
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[Image drawInRect:CGRectMake(0, 0, 100, 50)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
四、将一个UIView转换为UIImage
/** 将一个view制作为图片 */
- (UIImage *)captureView:(UIView *)view {
CGRect rect = view.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
五、存储图片到App沙盒内
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];