思路是建一个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[] = {15.0,5.0};
CGContextSetLineDash(ctx, , lengths, );
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(, , , )];
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(, , aRect.size.width, aRect.size.height)];
imgView.image = targetImg; //把截取得的图像显示到视图中去
imgView.tag=;
[self.view addSubview:imgView];
[imgView release];
} -(void)clear
{
UIImageView *imgView=(UIImageView *)[self.view viewWithTag:];
[imgView removeFromSuperview];
}

效果:

(IOS)截图Demo的更多相关文章

  1. 91平台iOS接入demo

    源码:http://pan.baidu.com/s/1DuBl6 今天整理硬盘,找到了一个有趣的demo.一年前,91助手游戏联运呈爆棚趋势,但是许多使用FlashAir开发的优秀的游戏和应用都卡在了 ...

  2. 03.WebView演练-iOS开发Demo(示例程序)源代码

    技术博客http://www.cnblogs.com/ChenYilong/   新浪微博http://weibo.com/luohanchenyilong   //转载请注明出处--本文永久链接:h ...

  3. XMPP协议实现即时通讯底层书写 (二)-- IOS XMPPFramework Demo+分析

    我希望,This is a new day! 在看代码之前,我认为你还是应该先整理一下心情,来听我说几句: 首先,我希望你是在早上边看这篇blog,然后一边開始动手操作,假设你仅仅是看blog而不去自 ...

  4. 适合新人学习的iOS官方Demo

    UICatalog.包括了绝大部分经常使用的UI,入门必备良药. 9  分段选择器 10滑动条 Slider 11stack view 12 分步条 13 开关 14 textfield 15text ...

  5. iOS商城demo、音乐播放器、视频通话、自定义搜索、转场动画等源码

    iOS精选源码 微信自定义搜索框实现 一个商城Demo,持续更新中 在Object-C中学习数据结构与算法之排序算法 iOS 音乐播放器之锁屏歌词+歌词解析+锁屏效果 XLsn0wPushTimePi ...

  6. iOS 截图功能

    步骤: 当我们所需截的图的大小超过我们屏幕的大小时,可以用UIScrollView作为底图,这样就可以截图我们所需的大小,即 UIScrollView *scrollView = self.view. ...

  7. iOS截图

    1.普通的截图办法,在View上面截图 /** * 截图代码 * * @param view 需要截图的view * @param rect 需要截取的区域 * * @return 返回截取的对象 * ...

  8. iOS Sqlite3 Demo 及 FMDB Demo

    本文是主要实现了三个函数: testSQLite3 是测试系统自带的sqlite3的demo testFMDB是测试FMDB存取简单的数据类型的 的demo testFMDB2是将任意对象作为一个整体 ...

  9. iOS多线程 iOS开发Demo(示例程序)源代码

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版)   iOS程序源代码下载链接:01.大任务.zip22 ...

随机推荐

  1. Codeforces 700B Connecting Universities(树形DP)

    [题目链接] http://codeforces.com/problemset/problem/700/B [题目大意] 给出 一棵n个节点的树, 现在在这棵树上选取2*k个点,两两配对,使得其配对的 ...

  2. nefu 462 fib组合

    nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.ed ...

  3. hdu acm 2154(多解取一解)

    //题目中结果有一条限制就是最后必须跳回A,如果我们的思想框在这个条件上就很容易卡住,因为这样的条件下的路径很难有规律的罗列,然而我们说这个图形中有三个区域,我们算出每个区域的第n-1次的种类数,然后 ...

  4. OpenCV 开发环境环境搭建(win10+vs2015+opencv 3.0)

    OpenCV 3.0 for windows(下载地址:http://opencv.org/): 本测试中,OpenCV安装目录:D:\Program Files\opencv,笔者操作系统为64位. ...

  5. C#隐藏桌面图标和任务栏

    最近因为项目需要需要实现桌面图标和任务状态栏的隐藏功能,实现的方式很多,比如修改注册表值,调用windows API函数等.经过一番的查阅,这个功能暂时实现了,或许不是很好的方法,但是我预期的效果达到 ...

  6. BZOJ 2440 完全平方数(莫比乌斯反演+二分查找)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23362 题意:定义含有平方数因子的数为完全平方数(平方数因子不包含 ...

  7. BZOJ 2208: [Jsoi2010]连通数( DFS )

    n只有2000,直接DFS就可以过了... -------------------------------------------------------------------------- #in ...

  8. 经典union的使用

    一个用户下广告位  某一天有收入和支出  有支出不一定有收入  有收入不一定有支出  下例为按用户查询 sanhao 下的信息 支出如下: 收入如下: 按天进行查询,例如查询: 得到结果如下: 使用一 ...

  9. [LeetCode]题解(python):135-Candy

    题目来源: https://leetcode.com/problems/candy/ 题意分析: 有N个孩子站成一条线.每个孩子有个排名.要求1.每个孩子至少一个糖果,2.相邻的孩子,那么较高排名的孩 ...

  10. [LeetCode]题解(python):076-Minimum Window Substring

    题目来源: https://leetcode.com/problems/minimum-window-substring/ 题意分析: 给定两个字符串S和T.在S中找到最短的一个子字符串使得他包括所有 ...