前一段时间由于公司项目需求(租车项目,需要验车拍照,标记车辆外观的损伤情况)需要对照片进行涂鸦处理,所以就简单研究了一下。示例demo。
主要是通过UIview的- (void)drawRect:(CGRect)rect 方法实现,通过点击手势来进行简单涂鸦。
//顶部图片手势
- (void)origviewGesture:(UITapGestureRecognizer *)Recognizer{
if (self.currentExmpleImgName) {
CGPoint point = [Recognizer locationInView:self.origview];
CGRect rect = CGRectMake(point.x- 15* 0.5, point.y- 15* 0.5, 15, 15);
NSLog(@"%@", NSStringFromCGPoint(point));
for (NSDictionary *dic in self.allScrArray) {
CGRect rectTmp = CGRectFromString(dic[@"rect"]);
BOOL contains = CGRectContainsPoint(rectTmp, point);
if (contains) {
[self.allScrArray removeObject:dic];
self.origview.imageArray = self.allScrArray;
return;
}
}
NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] init];
NSString *imgPoint = NSStringFromCGPoint(point);
NSString *imgRect = NSStringFromCGRect(rect);
[tmpDic setValue:imgRect forKey:@"rect"];
[tmpDic setValue:imgPoint forKey:@"Point"];
[tmpDic setValue:self.currentExmpleImgName forKey:@"imageName"];
[self.allScrArray addObject:tmpDic];
self.origview.imageArray = self.allScrArray;
}else {
return;
}
}
for循环主要判断重复点击的位置,如果点击的位置已经涂鸦,就清除当前的,如果没有就先添加涂鸦。
//保存
- (void)saveBtnAction{
//画布图片存储
UIGraphicsBeginImageContext(self.origview.frame.size);
[self.origview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = [[NSData alloc]init];
imageData = UIImageJPEGRepresentation(viewImage, 0.5);
self.resultImag.image = viewImage;
}
直接将涂鸦照片生成UIImage。demo有视频操作。