近日项目做开发时,利用了绘图技术。做一下绘图总结
1.代码为什么要写在 drawrect 当中
因为在这个方法当中可以获取到正确的上下文2.rect 参数的含义
当前 view 的 bounds,需要重绘的区域3.drawrect 什么时候调用?
这个方法是系统调用!!!
(1)当着 view 第一次显示的时候 会调用
(2)当这个 view 进行重绘的时候 会调用4.如何重绘
(1)调用某个需要重绘的 view 对象的 setNeedsDisplay
(2)调用某个需要重绘的 view 对象的 setNeedsDisplayInRect5.为什么不能手动调用 drawrect
手动调用的时候可能获取不到正确的上下文下面是一个截图片的案例
// 3.获取图片
UIImage* image = [UIImage imageNamed:@"me"];
// 1.开启图片类型的图形上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 5.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 4.画一个裁剪的图片
CGContextAddArc(ctx, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5, 0, 2 * M_PI, 1);
// 6.裁剪
CGContextClip(ctx);
// 7.把图片画上去
[image drawAtPoint:CGPointZero];
// 8.取出来
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// 2.关闭图片类型的图形上下文
UIGraphicsEndImageContext();
// 测试
self.imageView.image = newImage;
- 添加图片的案例
// 3.获取图片
UIImage* image = [UIImage imageNamed:@"scene"];
// 1.开启图片类型的图形上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); // 6.画大图
[image drawAtPoint:CGPointZero];
// 4.文字
NSString* str = @"黑马13期";
// 5.画文字水印
[str drawAtPoint:CGPointMake(20, 20) withAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:20] }];
// 7.图片
UIImage* logo = [UIImage imageNamed:@"logo"];
// 8.画图片水印
[logo drawAtPoint:CGPointMake(image.size.width * 0.6, image.size.height * 0.7)];
// 取图片
image = UIGraphicsGetImageFromCurrentImageContext();
[image drawAtPoint:CGPointZero];
// 保存到相册
UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);
// 2.关闭图片类型的图形上下文
UIGraphicsEndImageContext();
我项目的案例类似于图片上添加图片
- 调用两个方法分别创建部分视图,在拼接到一起
UIImage * img1 = [self imageWithName:@"But_hilight" size:CGSizeMake(100, 25) withCarName:model.carplate];
UIImage * img2 = [self imageWithName:@"icon_rail" size:CGSizeMake(30, 30)];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 60), NO, 0);
[img1 drawInRect:CGRectMake(0, 5, 100, 25)];
[img2 drawInRect:CGRectMake(40, 30, 30, 30)];
UIImage *Annotationimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
- (UIImage *)imageWithName:(NSString *)imgName size:(CGSize)size withCarName:(NSString*) carName {
if (!imgName || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
UIImage *img = [UIImage imageNamed:imgName];
[img drawAtPoint:CGPointZero];
NSString *str = carName;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; // 文字颜色
dict[NSFontAttributeName] = [UIFont systemFontOfSize:18]; // 字体
[str drawInRect:CGRectMake(10, 1, size.width, size.height) withAttributes:dict];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)imageWithName:(NSString *)imgName size:(CGSize)size {
if (!imgName || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
UIImage *img = [UIImage imageNamed:imgName];
[img drawAtPoint:CGPointZero];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}