- 效果图
![这里写图片描述](https://img-blog.csdn.net/20150805231941354)
- 代码实现,自定义ImageView
#import "ZJCutImageView.h"
@interface ZJCutImageView ()
@property (nonatomic, weak) UIView *panView;
@property (nonatomic, assign) CGPoint oriP;
@end
@implementation ZJCutImageView
-(UIView *)panView{
if (!_panView) {
UIView *coverV = [[UIView alloc] init];
coverV.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
_panView = coverV;
[self addSubview:coverV];
}
return _panView;
}
-(void)awakeFromNib{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
self.userInteractionEnabled = YES;
}
-(void)pan:(UIPanGestureRecognizer *)pan{
CGPoint curP = [pan locationInView:self];
if(pan.state == UIGestureRecognizerStateBegan){
_oriP = curP;
}
self.panView.frame = CGRectMake(_oriP.x, _oriP.y, curP.x - _oriP.x, curP.y -_oriP.y);
if (pan.state == UIGestureRecognizerStateEnded) {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.panView.frame];
[path addClip];
[_panView removeFromSuperview];
[self.layer renderInContext:ctx];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
@end