博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(IOS)截图Demo
阅读量:5127 次
发布时间:2019-06-13

本文共 3029 字,大约阅读时间需要 10 分钟。

思路是建一个UIView的子类,获取划动出的矩形,用协议将矩形传递给代理对象,依据该矩形完成图像数据的截取,并显示出来。

截图视图类:

#import 
@protocol UICutImgDelegate;@interface BIDCutView : UIView{ CGPoint startPoint; CGRect targetRect; id
_delegate;}@property (assign , nonatomic) id delegate;@end@protocol UICutImgDelegate
-(void)cutImgWithRect:(CGRect) aRect;-(void)clear;@end

 

#import "BIDCutView.h"@implementation BIDCutView@synthesize delegate=_delegate;- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code    }    return self;}- (void)drawRect:(CGRect)rect{    CGContextRef ctx = UIGraphicsGetCurrentContext();    CGContextSetLineWidth(ctx, 1.5);    CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);    CGFloat lengths[2] = {
15.0,5.0}; CGContextSetLineDash(ctx, 2, lengths, 2); CGContextStrokeRect(ctx, targetRect); //画虚线矩形}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.delegate clear]; startPoint=[[touches anyObject] locationInView:self];}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint currentPoint=[[touches anyObject] locationInView:self]; targetRect = CGRectMake(startPoint.x, startPoint.y, currentPoint.x-startPoint.x, currentPoint.y-startPoint.y); [self setNeedsDisplay];}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if (self.delegate && [self.delegate respondsToSelector:@selector(cutImgWithRect:)]) { [self.delegate cutImgWithRect:targetRect]; }} @end

 

视图控制器:(作为截图视图的代理对象)

#import 
#import "BIDCutView.h"@interface BIDRootViewController : UIViewController
@end

 

#import "BIDRootViewController.h"#import "BIDSimpleTouchFun.h"#import "BIDDiscount.h"@implementation BIDRootViewController-(void)loadView{    [super loadView];   //self.view=[[[BIDDrawViewalloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
BIDCutView *cutView=[[BIDCutView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];    cutView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"abc.jpg"]];    cutView.delegate = self;    [self.view addSubview:cutView];    [cutView release];}- (void)viewDidLoad{    [super viewDidLoad];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}-(void)cutImgWithRect:(CGRect)aRect{    UIImage *img=[UIImage imageNamed:@"abc.jpg"];    CGImageRef imgRef = img.CGImage;    CGImageRef targetImgRef = CGImageCreateWithImageInRect(imgRef, aRect);  //图像的截取    UIImage *targetImg=[UIImage imageWithCGImage:targetImgRef];        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width, aRect.size.height)];    imgView.image = targetImg; //把截取得的图像显示到视图中去    imgView.tag=1000;    [self.view addSubview:imgView];    [imgView release];}-(void)clear{    UIImageView *imgView=(UIImageView *)[self.view viewWithTag:1000];    [imgView removeFromSuperview];}

效果: 

转载于:https://www.cnblogs.com/mingfung-liu/p/3230038.html

你可能感兴趣的文章
接口测试用例设计
查看>>
WebConfig配置文件详解(转载自逆心的博客)
查看>>
Ex3_28 在2SAT问题中,给定一个字句的集合..._第十二次作业
查看>>
如何截取iframe的内容,修改他的CSS
查看>>
telnet测试端口是否正常打开
查看>>
python中的__new__、__init__和__del__
查看>>
PHP使用缓存提高网站性能
查看>>
用C#实现智能设备上的NotifyIcon类
查看>>
项目实施(二)
查看>>
HDU 1045 Fire Net
查看>>
Github
查看>>
cmake 手册详解【转】
查看>>
一般在页面上添加隐藏域用来接受设置一些值方便开发
查看>>
net 表格控件
查看>>
CodeForces Round 197 Div2
查看>>
boost-使用format和lexical_cast实现数字和字符串之间的转换
查看>>
Learn a Linux command every day--day2:ls命令
查看>>
java集合的三种遍历方式
查看>>
Visual formatting model
查看>>
木马分析(隐藏分析)实验
查看>>