1.利用Quartz2D画文字
- (void)drawRect:(CGRect)rect
CGContextRef ctx = UIGraphicsGetCurrentContext();
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:18];
NSString *str = @"我是哈哈 strong good morning";
[str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:dict];
CGContextStrokePath(ctx);
}
2.利用Quartz2D画图片
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIImage *img = [UIImage imageNamed:@"img1"];
[img drawAsPatternInRect:self.bounds];
NSString *str = @"糖伯虎著";
[str drawAtPoint:CGPointMake(10, 200) withAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
CGContextStrokePath(ctx);
}
3.利用Quartz2D制作水印
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *bgImage = [UIImage imageNamed:@"scene"];
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
[bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
UIImage *waterImage = [UIImage imageNamed:@"logo"];
CGFloat margin = 5;
CGFloat scale = 0.2;
CGFloat waterImageW = waterImage.size.width * scale;
CGFloat waterImageH = waterImage.size.height * scale;
CGFloat waterImageX = bgImage.size.width - waterImageW - margin;
CGFloat waterImageY = bgImage.size.height - waterImageH - margin;
[waterImage drawInRect:CGRectMake(waterImageX, waterImageY, waterImageW, waterImageH)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.iv.image = newImage;
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[data writeToFile:path atomically:YES];
}
4.利用Quartz2D进行图片裁剪
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *me = [UIImage imageNamed:@"me"];
UIGraphicsBeginImageContextWithOptions(me.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, me.size.width, me.size.height);
CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx);
[me drawInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[data writeToFile:path atomically:YES];
self.iv.image = newImage;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *me = [UIImage imageNamed:@"me"];
CGFloat borderW = 2;
CGFloat imageW = me.size.width + borderW * 2;
CGFloat imageH = me.size.height + borderW * 2;
CGSize newSize = CGSizeMake(imageW, imageH);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddArc(ctx, me.size.width * 0.5 + borderW , me.size.width * 0.5 + borderW, me.size.width * 0.5 + borderW, 0, M_PI * 2, 0);
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextFillPath(ctx);
CGRect rect = CGRectMake(borderW, borderW, me.size.width, me.size.height);
CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx);
[me drawInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.iv.image = newImage;
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[data writeToFile:path atomically:YES];
}