实现的效果如图:
首先要导入的是QuartzCore.framework框架
#import "ViewController.h"
#import "SkipView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
SkipView *myView =[[SkipViewalloc] initWithFrame:CGRectMake(0,0, 320, 480-20)];
[self.viewaddSubview:myView];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
@end
UIView
#import <UIKit/UIKit.h>
@interface SkipView : UIView
@property (assign,nonatomic) CGPoint aPoint;//始点
@property (assign,nonatomic) CGPoint bPoint;//终点
@property (retain,nonatomic) UIImageView *myImgView;//图形框架
@end
#import "SkipView.h"
#import <QuartzCore/QuartzCore.h>
@implementation SkipView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"che.jpg"]];//设置view的背景图片
self.myImgView = [[UIImageViewalloc] init];//初始化视图框
[self addSubview:self.myImgView];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];//获取初始触摸对象
self.aPoint = [touchlocationInView:self];//获取触摸在视图上的位置
NSLog(@"%@",NSStringFromCGPoint(self.aPoint));
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];//获取最后的任意位置
self.bPoint = [touchlocationInView:self];//获取触摸在视图上的位置
NSLog(@"%@",NSStringFromCGPoint(self.bPoint));
[selfsetNeedsDisplay];//设置刷新界面
//截取图片方法,截取图片返回给image
UIImage*image=[selfcaptureView:nil];
//截取的图片大小
self.myImgView.frame=CGRectMake(self.aPoint.x,self.aPoint.y,self.bPoint.x-self.aPoint.x,self.bPoint.y-self.aPoint.y);
//设置图片视图框的图片为截取的图片
[self.myImgViewsetImage:image];
[UIViewanimateWithDuration:1animations:^{
self.myImgView.frame=CGRectMake(0,0, image.size.width, image.size.height);
}];
}
// Drawing pictures
- (void)drawRect:(CGRect)rect
{
CGContextRef ref =UIGraphicsGetCurrentContext();//获取上下文对象
CGContextSetLineWidth(ref,2.0f);//设置绘图线框
CGContextSetStrokeColorWithColor(ref, [UIColorredColor].CGColor);//设置线框的颜色
CGFloat a[]={10,10};//给定一个数组
/*
*CGContextRef c 上下文
*CGFloat phase 跳过多少个点
*const CGFloat *lengths 用数组表示一个矩形间距
*size_t count 数组大小
*CGContextSetLineDash(CGContextRef c, CGFloat phase, const CGFloat *lengths, size_t count);
*/
CGContextSetLineDash(ref,5, a, 2);//设置虚线的样式
CGContextMoveToPoint(ref,self.aPoint.x,self.aPoint.y);//设置起始点
CGContextAddRect(ref,CGRectMake(self.aPoint.x,self.aPoint.y,self.bPoint.x-self.aPoint.x,self.bPoint.y-self.aPoint.y));//设置矩形框的位置大小
CGContextStrokePath(ref);//开始绘制图形
}
//截图片的方法
-(UIImage*)captureView:(UIView *)theView
{
CGRect rect = self.frame;
UIGraphicsBeginImageContext(rect.size);//获取图形上下文
CGContextRef context =UIGraphicsGetCurrentContext();
[self.layerrenderInContext:context];//这里需导入#import <QuartzCore/QuartzCore.h>还有QuartzCore框架
UIImage *img =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect1 = CGRectMake(self.aPoint.x,self.aPoint.y,self.bPoint.x-self.aPoint.x,self.bPoint.y-self.aPoint.y);//创建矩形框
UIImage * im = [UIImageimageWithCGImage:CGImageCreateWithImageInRect([imgCGImage], rect1)];
return im;
}
@end