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

截图视图类:

#import <UIKit/UIKit.h>@protocol UICutImgDelegate;@interface BIDCutView : UIView
{CGPoint startPoint;CGRect targetRect;id <UICutImgDelegate> _delegate;
}
@property (assign , nonatomic) id delegate;
@end@protocol UICutImgDelegate <NSObject>
-(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 <UIKit/UIKit.h>
#import "BIDCutView.h"@interface BIDRootViewController : UIViewController <UICutImgDelegate>@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

(IOS)截图Demo相关推荐

  1. flex+php截图Demo

    在flex中使用了两种方案来处理图片: 一.直接将byteArray转为bitmap通过loader(flash.display.Loader)显示在舞台上: 二.将byteArray调用将三方方法( ...

  2. 一些iOS实用Demo

    图像浏览及处理 FLAnimatedImage - gif播放处理的工具. CLImageEditor - 超强的图片编辑库,快速帮你实现旋转,防缩,滤镜等等一系列麻烦的事情. ios-image-f ...

  3. 分享一些iOS实用Demo源码

    总结了一些常用的demo,包括获取验证码.AVPlayer.AFNetworking.Masonry.高仿微信.高仿网易.K线图.折线图.柱状图.手势解锁.TouchID.直播.动画等,大家可以根据需 ...

  4. iOS 截图的那些事儿

    同时按下 Home 键和电源键,咔嚓一声,就得到了一张手机的截图,这操作想必 iPhone 用户再熟悉不过了.我们作为研发人员,面对的是一个个的 View,那么该怎么用代码对 View 进行截图呢? ...

  5. Unity iOS截图并保存到手机相册总结

    Unity iOS捕捉相机拍到的画面并截图保存到手机相册总结 本文专门针对小白,详细(图文)的写了通过unity 3d实现ios捕捉相机拍到的画面并截图保存到手机相册的方法.(对大佬来说可能写的略显繁 ...

  6. python朋友圈自动点赞_基于AirTest+Python的ios自动化测试demo(微信朋友圈无限点赞)...

    AirTest相比Appuim有个好处就是可以对GUI图片进行捕捉和最新版本支持WebView(目前Appuim不支持iOS12的WebView进行Xpath抓取) AirTest环境搭建可参考以下链 ...

  7. python自动点赞微信_基于AirTest+Python的ios自动化测试demo(微信朋友圈无限点赞)

    AirTest相比Appuim有个好处就是可以对GUI图片进行捕捉和最新版本支持WebView(目前Appuim不支持iOS12的WebView进行Xpath抓取) AirTest环境搭建可参考以下链 ...

  8. iOS 截图 截取屏幕

    // 从view上截图 - (UIImage *)getImage {UIGraphicsBeginImageContextWithOptions(CGSizeMake(150, 150), NO, ...

  9. iOS 截图 对view指定区域 以及 对图片 指定区域截图

    //MARK:获取截图,对view 制定区域截图static func getCropImage(_ view:UIView,size:CGSize) -> UIImage {//false是透 ...

最新文章

  1. C语言字符串哪个头文件,如果在程序中要使用C语言的字符串处理函数,应在程序中包含这哪个头文件。...
  2. matlab Normally distributed random numbers in two columns
  3. Oracle 的一些语句
  4. 条款46:需要类型转换的时候请为模板定义非成员函数
  5. 最强Java面试题全部合集,涵盖BAT大厂面试必考的9大技术!-强烈建议收藏
  6. fscanf不读取_NCNN-Breakdown(3) 读取网络的proto信息
  7. repo sync代码断点续传
  8. 机器学习中的数学——概率论基础知识
  9. 华为发布IdeaHub S2系列,与华为云会议结合更强大
  10. Redhat7.3修改密码
  11. 火星坐标-84坐标-百度地图坐标相互转换
  12. 人工智能3.0展望---一种让AI产生自我意识的方法
  13. 达梦数据库培训心得及DCA考试总结
  14. OpenCV可以识别文字吗?
  15. python训练好的图片验证_Python图片验证码降噪处理实例
  16. 哈密瓜水果的文案,水果哈密瓜文案高级感
  17. 黄褐斑的食疗用什么,姬净美效果不错
  18. Android webview加载页面获取摄像头权限实践(Kotlin)
  19. 基于java大学生创业众筹系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
  20. xp系统开启 服务器配置,xp系统开启 服务器配置

热门文章

  1. 20180307:python接口测试时json的传参与解析区分
  2. 查看WEB服务器的连接数
  3. CAEmitterLayer 和 CAEmitterCell 粒子发射
  4. 【100亿次的挑战】之春晚控制后台故事分享
  5. 东北考生到南方学计算机,为啥东北考生都想去南方,而南方学生很少考东北,看看他们怎么说...
  6. URLEncoder 、URLDecoder 对中文转码解码使用
  7. Android 微信登录
  8. .net core2.0 Memcached.ClientLibrary
  9. django设置超级管理员_Django的简介与安装
  10. 上海2014计算机职称考试理论题,2014年上海市高校计算机一级考试8套理论题含答案...