思路是建一个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. java中的集合链表

    java中的集合类有很多种,每个都有自己的一些特点,推荐你专门在这方面研究一下,比方Vector,ArrayList,,LinkedList,Hashtable等,其中你问到的链表,是不是指Linke ...

  2. InnoDB引擎Myslq数据库数据恢复

    首先祝愿看到这片文章的你永远不要有机会用到它... 本文指针对用InnoDB引擎的Mysql数据库的数据恢复,如果是其它引擎的Mysql或其它数据库请自行google... 如果有一天你手挫不小心删掉 ...

  3. 传统web和mvc的区别

  4. Spring 中拦截器与过滤器的区别

    spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...

  5. nginx+uwsgi部署python web(web.py)

    1.nginx: nginx 是一个 http 服务器,与 apache.lighttpd.Microsoft IIS 等属于同类产品. 2.uWSGI: uWSGI 是一个快速的.纯C语言开发的.自 ...

  6. 【Android】手机号码获取问题

    手机号码不是所有的都能获取.只是有一部分可以拿到.这个是由于移动运营商没有把手机号码的数据写入到sim卡中.SIM卡只有唯一的编号,供网络与设备识别那就是IMSI号码,手机的信号也可以说是通过这个号码 ...

  7. 无法在web服务器上启动调试,此项目在使用一个被配置为使用特定IP地址的网站。请在项目URL中指定计算机名称。

    解决方案:IIS服务器管理-default web site 绑定-选择使用的IP-编辑-IP地址:全部未匹配

  8. HTML+CSS笔记 CSS中级 一些小技巧

    水平居中 行内元素的水平居中 </a></li> <li><a href="#">2</a></li> &l ...

  9. Hash table in PowerShell

    hashtable is easy to create, access and manipulate. we simply use $hashTable = @{} to create an empt ...

  10. 指针和引用区别 C++

    #include <iostream> using namespace std; int main(){ ; void cubeByPoint(int *);//指针传参声明 void c ...