手势、画图、截图
来源:互联网 发布:mac的办公软件 编辑:程序博客网 时间:2024/05/01 03:56
实现的效果如图:
首先要导入的是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
- 手势、画图、截图
- ios drawrect 画图,截图
- iOS 画图 截图
- js,html5画图,js截图,ajax上传
- 高效工作---高效利用快捷键实现截图与画图
- 画图:
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- 画图
- POJ_1753
- 去掉字符串中的数字
- Java内存泄露小例子
- LeetCode题解:3sum closest
- 用位运算生成分形
- 手势、画图、截图
- Debian 7 下搭建 IPSEC + L2TP VPN 服务器
- Java中的Set,List,Map的区别
- Linux内核访问I/O资源的方法:动态映射(ioremap)和静态映射(map_desc)
- android手机连不上电脑总结
- 堆、栈、BSS、Data、code区、静态存储区、文字常量区
- 关于操作数关系
- Vim清空文件
- linux学习笔记1