(IOS)截图Demo
思路是建一个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的更多相关文章
- 91平台iOS接入demo
源码:http://pan.baidu.com/s/1DuBl6 今天整理硬盘,找到了一个有趣的demo.一年前,91助手游戏联运呈爆棚趋势,但是许多使用FlashAir开发的优秀的游戏和应用都卡在了 ...
- 03.WebView演练-iOS开发Demo(示例程序)源代码
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong //转载请注明出处--本文永久链接:h ...
- XMPP协议实现即时通讯底层书写 (二)-- IOS XMPPFramework Demo+分析
我希望,This is a new day! 在看代码之前,我认为你还是应该先整理一下心情,来听我说几句: 首先,我希望你是在早上边看这篇blog,然后一边開始动手操作,假设你仅仅是看blog而不去自 ...
- 适合新人学习的iOS官方Demo
UICatalog.包括了绝大部分经常使用的UI,入门必备良药. 9 分段选择器 10滑动条 Slider 11stack view 12 分步条 13 开关 14 textfield 15text ...
- iOS商城demo、音乐播放器、视频通话、自定义搜索、转场动画等源码
iOS精选源码 微信自定义搜索框实现 一个商城Demo,持续更新中 在Object-C中学习数据结构与算法之排序算法 iOS 音乐播放器之锁屏歌词+歌词解析+锁屏效果 XLsn0wPushTimePi ...
- iOS 截图功能
步骤: 当我们所需截的图的大小超过我们屏幕的大小时,可以用UIScrollView作为底图,这样就可以截图我们所需的大小,即 UIScrollView *scrollView = self.view. ...
- iOS截图
1.普通的截图办法,在View上面截图 /** * 截图代码 * * @param view 需要截图的view * @param rect 需要截取的区域 * * @return 返回截取的对象 * ...
- iOS Sqlite3 Demo 及 FMDB Demo
本文是主要实现了三个函数: testSQLite3 是测试系统自带的sqlite3的demo testFMDB是测试FMDB存取简单的数据类型的 的demo testFMDB2是将任意对象作为一个整体 ...
- iOS多线程
iOS开发Demo(示例程序)源代码
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版) iOS程序源代码下载链接:01.大任务.zip22 ...
随机推荐
- oracle 11g RAC Grid Infrastructure
grid infrastructure 软件介质下载: http://www.oracle.com/technetwork/database/database-technologies/cluster ...
- Android 开发佳站【转】
Android控件拖动 这里演示控件拖动的动画: 原理就是响应控件的Touch事件,在Touch事件中对移动进行处理,注意,一定要在布局文件中设置控件的 android:clickable=& ...
- 不是技术牛人,如何拿到国内IT巨头的Offer(1)
转自:http://developer.51cto.com/art/201404/436685.htm 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,N ...
- HDU 4861 Couple doubi(找规律|费马定理)
Couple doubi Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- JavaMail收发邮件的一般流程与主要方法
1.Properties属性类 Properties p = new Properties(); p.put(key, value); key -| mail.smtp.host -| mail.sm ...
- hadoop默认3个核心配置文件说明
1 获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知 ...
- iOS实践03
主要目标:版本新特性界面,新浪授权界面(登录界面)的处理 任务基本完成了,基本的框架也就到这了,接下来的应该是首页获取微博了. 1.版本新特性,可以单独作为一个model,写完之加入到项目中.我们新建 ...
- 关于Staruml与powerdesigner启动使用中的问题
问题描述:启动StarUML时,报System Error.Code:1722.RPC服务器不可用的错误! 如下: 这时候: 只需要开启Print Spooler服务即可!在“控制面板中-->管 ...
- 精简版LVCL,有空看看
http://tothpaul.free.fr/sources.php?lvcl.lvcl1 http://synopse.info/forum/viewtopic.php?id=665
- hdu 5269 ZYB loves Xor I(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269 思路分析:当lowbit(AxorB)=2p 时,表示A与B的二进制表示的0-p-1位相等,第p ...