主要是看看图形上下文
如果你看到了这里
内容不多,包括
水印:给图片添加水印(文字和图片水印)裁剪:裁剪圆形图片(带边框或者不带)截屏:截取当前屏幕擦除:这个不知道怎么描述....
不知怎么描述擦除
. .
OK,为了方便使用,我都写在Image的分类中了,拿出来就可以使用!
. . . . . .
1、图形上下文:主要是对图片进行处理,操作步骤基本如下,可在 2 之前或者之后对上下文进行处理
1 开启一个图形上下文2 绘制图片3 从当前上下文获取新的图片4 关闭上下文
+ (
UIImage *)pq_drawImageWithImageNamed:(
NSString *)name{
UIImage *image = [
UIImage imageWithContentsOfFile:[[
NSBundle mainBundle] pathForResource:name ofType:
nil]];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:
CGRectMake(
0,
0, image.size.width, image.size.height)];
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
3.png
. . . .
2、给图片添加文字水印:
1 开启一个图形上下文2 绘制图片3 把文字绘制到当前上下文4 从当前上下文获取新的图片5 关闭上下文
+ (UIImage *)pq_WaterImageWithImage:(UIImage *)
image text:(NSString *)
text textPoint:(CGPoint)
point attributedString:(NSDictionary * )attributed{
UIGraphicsBeginImageContextWithOptions(
image.
size, NO,
0);
[
image drawInRect:CGRectMake(
0,
0,
image.
size.
width,
image.
size.
height)];
[
text drawAtPoint:
point withAttributes:attributed];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
4.png
. . . .
3、给图片添加图片水印:
1 开启一个图形上下文2 绘制图片3 把水印图片绘制到当前上下文4 从当前上下文获取新的图片5 关闭上下文
+ (UIImage *)pq_WaterImageWithImage:(UIImage *)
image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)
rect{
UIGraphicsBeginImageContextWithOptions(
image.
size, NO,
0);
[
image drawInRect:CGRectMake(
0,
0,
image.
size.
width,
image.
size.
height)];
[waterImage drawInRect:
rect];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
5.png
. . . .
4、裁剪圆形图片:
1 开启一个图形上下文2 设置裁剪区域3 把图片绘制到当前上下文4 从当前上下文获取新的图片5 关闭上下文
+ (
nullable UIImage *)pq_ClipCircleImageWithImage:(
nullable UIImage *)image circleRect:(
CGRect)rect{
UIGraphicsBeginImageContextWithOptions(image.size,
NO,
0);
UIBezierPath * path = [
UIBezierPath bezierPathWithOvalInRect:rect];
[path addClip];
[image drawAtPoint:
CGPointZero];
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
6.png
. . . .
5、裁剪带边框的圆形图片:
1 开启一个图形上下文2 设置边框3 设置裁剪区域4 把图片绘制到当前上下文5 从当前上下文获取新的图片6 关闭上下文
+ (
nullable UIImage *)pq_ClipCircleImageWithImage:(
nullable UIImage *)image circleRect:(
CGRect)rect borderWidth:(
CGFloat)borderW borderColor:(
nullable UIColor *)borderColor{
UIGraphicsBeginImageContext(image.size);
UIBezierPath * path = [
UIBezierPath bezierPathWithOvalInRect:rect];
[borderColor setFill];
[path fill];
UIBezierPath * clipPath = [
UIBezierPath bezierPathWithOvalInRect:
CGRectMake(rect.origin.x + borderW , rect.origin.x + borderW , rect.size.width - borderW *
2, rect.size.height - borderW *
2)];
[clipPath addClip];
[image drawAtPoint:
CGPointZero];
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
7.png
. . . .
6、截屏:
1 开启一个图形上下文2 获取当前上下文3 截屏,渲染到当前上下文中,这里使用绘制无效,可自行测试4 从当前上下文获取新的图片5 把图片转化成为NSData类型6 关闭上下文7 把新的图片和NSData类型直接放回,便于显示和保存截屏
+ (
void)pq_cutScreenWithView:(
nullable UIView *)view successBlock:(
nullable void(^)(
UIImage * _Nullable image,
NSData * _Nullable imagedata))block{
UIGraphicsBeginImageContext(view.bounds.size);
CGContextRef ctx =
UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
NSData * data =
UIImageJPEGRepresentation(newImage,
1);
UIGraphicsEndImageContext();
block(newImage,data);
}
8.png
9.png
. . . .
7、擦除:
10.png
1 计算当前擦除区域的大小位置:
11.png
2 开启上下文
3 获取当前上下文
4 把图片绘制到当前上下文
5 裁剪出透明区域
6 得到当前上下文的图片
7 关闭上下文
8 把图片设置给前景ImageView
- (
void)wipePanGestureEvent:(
UIPanGestureRecognizer * )pan{
CGPoint nowPoint = [pan locationInView:
self.wipeImageV];
CGFloat offsetX = nowPoint.x -
10;
CGFloat offsetY = nowPoint.y -
10;
CGRect clipRect =
CGRectMake(offsetX, offsetY,
20,
20);
UIGraphicsBeginImageContextWithOptions(
self.wipeImageV.bounds.size,
NO,
1);
CGContextRef ctx =
UIGraphicsGetCurrentContext();
[
self.wipeImageV.layer renderInContext:ctx];
CGContextClearRect(ctx, clipRect);
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.wipeImageV.image = newImage;
}
为了代码能复用,作者再次把这个代码封装到了Image类别中:
- (
nullable UIImage *)pq_wipeImageWithView:(
nullable UIView *)view currentPoint:(
CGPoint)nowPoint size:(
CGSize)size{
CGFloat offsetX = nowPoint.x - size.width *
0.5;
CGFloat offsetY = nowPoint.y - size.height *
0.5;
CGRect clipRect =
CGRectMake(offsetX, offsetY, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(view.bounds.size,
NO,
1);
CGContextRef ctx =
UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
CGContextClearRect(ctx, clipRect);
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
外面直接调用
self.wipeImageV.image = [
self.wipeImageV.image pq_wipeImageWithView:
self.wipeImageV currentPoint:[pan locationInView:
self.wipeImageV] size:
CGSizeMake(
20,
20)];
效果图如下: 没擦除之前:
擦除之前
擦除之后:
擦除之后
效果图
. . . .
8、图片裁剪:
之前都是固定区域、固定形状的裁剪,现在我们做一个自己选择大小,范围的裁剪。
走一遍思想(个人想法,如果你有更好的,欢迎指正):
思路
然后因为这个区域是自己控制大小的! 所以要建立一个View
移动:思路就是获取当前的点加上偏移量
图片
实现方法有两种,一种是添加Pan事件,一种直接重写touches系列方法 我采用后者。
- (
void)touchesBegan:(
NSSet<
UITouch *> *)touches withEvent:(
UIEvent *)event{
UITouch * touch = [touches anyObject];
_startP = [touch locationInView:
self];
}
- (
void)touchesMoved:(
NSSet<
UITouch *> *)touches withEvent:(
UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint curP = [touch locationInView:
self];
CGFloat x = curP.x - _startP.x;
CGFloat y = curP.y - _startP.y;
self.x += x;
if (
self.x <=
0) {
self.x =
0;
}
self.y += y;
if (
self.y <=
0) {
self.y =
0;
}
[
self ifOut];
}
- (
void)touchesEnded:(
NSSet<
UITouch *> *)touches withEvent:(
UIEvent *)event{
}
大小变换:
大小
- (
IBAction)sizePan:(
UIPanGestureRecognizer *)sender {
switch (sender.state) {
case UIGestureRecognizerStateBegan:
_sizeStartP = [sender locationInView:
self];
oldSize =
self.size;
break;
case UIGestureRecognizerStateChanged:
{
CGPoint curP = [sender locationInView:
self ];
CGFloat w = curP.x - _sizeStartP.x;
CGFloat h = curP.y - _sizeStartP.y;
self.width = oldSize.width + w;
self.height = oldSize.height + h;
[
self ifOut];
}
break;
default:
break;
}
}
剪切过程:
裁剪过程
+ (
void)pq_cutScreenWithView:(
nullable UIView *)view cutFrame:(
CGRect)frame successBlock:(
nullable void(^)(
UIImage * _Nullable image,
NSData * _Nullable imagedata))block{
for (PQWipeView * wipe
in view.subviews) {
[wipe setHidden:
YES];
}
UIGraphicsBeginImageContext(view.frame.size);
CGContextRef ctx =
UIGraphicsGetCurrentContext();
UIBezierPath * path = [
UIBezierPath bezierPathWithRect:frame];
[path addClip];
[view.layer renderInContext:ctx];
UIImage * newImage =
UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
for (PQWipeView * wipe
in view.subviews) {
[wipe setHidden:
NO];
}
UIGraphicsBeginImageContextWithOptions(frame.size,
NO,
0);
[newImage drawAtPoint:
CGPointMake(- frame.origin.x, - frame.origin.y)];
UIImage * fImage =
UIGraphicsGetImageFromCurrentImageContext();
NSData * data2 =
UIImageJPEGRepresentation(fImage,
1);
UIGraphicsEndImageContext();
block(fImage,data2);
}
这里在说说二次裁剪吧:
二次裁剪
效果图: 原始大小:
18.png
改变之后
19.png
点击裁剪:
20.png
裁减之后:
效果图
沙河路径:
终于写完啦!!!!