self.imageContext = CGBitmapContextCreate(0, frame.size.width, frame.size.height, 8, frame.size.width * 4, self.colorSpace, kCGImageAlphaPremultipliedLast); CGContextSetStrokeColorWithColor(self.imageContext,[UIColor redColor].CGColor); CGContextSetFillColorWithColor(self.imageContext, [UIColor redColor].CGColor); CGContextTranslateCTM(self.imageContext, 0.0f, self.bounds.size.height); CGContextScaleCTM(self.imageContext, 1.0f, -1.0f);
#pragma mark - touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
[self reCreateImageWithTouchDict:@{@"touch":touch, @"lineWidth":@(touch.majorRadius)}];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
[self reCreateImageWithTouchDict:@{@"touch":touch, @"lineWidth":@(touch.majorRadius)}];
}
- (UIImage *)reCreateImageWithTouchDict:(NSDictionary *)touchDict{
UITouch* touch = touchDict[@"touch"];
CGFloat lineWidth = [touchDict[@"lineWidth"] floatValue] * 0.5;
if (lineWidth < 1.0) {
lineWidth = 10;
}
if (touch) {
CGPoint point = [touch locationInView:touch.view];
if (touch.phase == UITouchPhaseBegan) {
CGRect rect = CGRectMake(point.x - lineWidth, point.y - lineWidth, lineWidth*2, lineWidth*2);
CGContextAddEllipseInRect(self.imageContext, rect);
CGContextFillPath(self.imageContext);
[self.points removeAllObjects];
[self.points addObject:[NSValue valueWithCGPoint:point]];
}else if (touch.phase == UITouchPhaseMoved){
[self.points addObject:[NSValue valueWithCGPoint:point]];
if (self.points.count > 2) {
CGContextSetLineCap(self.imageContext, kCGLineCapRound);
CGContextSetLineWidth(self.imageContext, 2 * lineWidth);
do{
CGPoint point0 = [(NSValue *)self.points[0] CGPointValue];
CGPoint point1 = [(NSValue *)self.points[1] CGPointValue];
CGContextMoveToPoint(self.imageContext, point0.x, point0.y);
CGContextAddLineToPoint(self.imageContext, point1.x, point1.y);
[self.points removeObjectAtIndex:0];
}while (self.points.count > 2);
}
}
CGContextStrokePath(self.imageContext);
}
CGImageRef cgImage = CGBitmapContextCreateImage(self.imageContext);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return image;
}
CALayer *mask = [CALayer layer]; mask.contents = (id)image.CGImage; mask.anchorPoint = CGPointZero; mask.frame = self.bounds; self.imageView.layer.mask = mask; self.imageView.layer.masksToBounds = YES;
- (void)dealloc{
if (_imageContext != NULL) {
CFRelease(_imageContext);
}
if (_colorSpace != NULL) {
CFRelease(_colorSpace);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有