您的位置:首页 > 其它

基于Quartz 2D实现截图功能

2015-10-22 20:04 337 查看
对于手机上的截屏功能,使用Quartz 2D绘制可以完成该功能,下面奉上小Demo仅供参考:

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"1.png"]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.记录当前手指的位置
UITouch *touch = [touches anyObject];
_beginPoint = [touch locationInView:self.view];
// 2.创建可一个可视的遮罩视图
if (_maskView == nil) {
_maskView = [[UIView alloc] initWithFrame:CGRectZero];
_maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
// 设置描边
_maskView.layer.borderWidth = 1;
_maskView.layer.borderColor = [UIColor blackColor].CGColor;
}
// 3.设置位置
_maskView.frame = CGRectMake(_beginPoint.x, _beginPoint.y, 1, 1);
// 添加到当前视图上
[self.view addSubview:_maskView];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取手指当前的位置计算出视图显示的大小
// 1.1 获取当前手指的位置
UITouch *_moveTouch = [touches anyObject];
CGPoint _movePoint = [_moveTouch locationInView:self.view];
// 1.2 当前遮罩视图的大小
float width = _movePoint.x - _beginPoint.x;
float height = _movePoint.y - _beginPoint.y;
// 1.3 设置给遮罩视图
_maskView.frame = CGRectMake(_beginPoint.x, _beginPoint.y, width, height);

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.获取当前视图的frame
// 2.移除当前遮罩视图
[_maskView removeFromSuperview];
// 3.获取指定区域内容内容
// 设置lay的锚点位置
self.view.frame = CGRectMake(-_maskView.frame.origin.x, -_maskView.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
// 3.1 开始绘制图片画布
UIGraphicsBeginImageContextWithOptions(_maskView.frame.size, YES, 0);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
// 3.2 获取整张图片大小
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 3.3 结束图片绘制
UIGraphicsEndImageContext();

self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
// 保存到本地相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

// 相册保存完成调用的方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error == nil) {
NSLog(@"截图图片成功");
} else {
NSLog(@"保存失败");
}
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.移除当前遮罩视图
}

/**
* 截取部分图像
*
**/
- (UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect centerBool:(BOOL)centerBool
{

/*如若centerBool为Yes则是由中心点取mCGRect范围的图片*/

float imgwidth = image.size.width;
float imgheight = image.size.height;
float viewwidth = mCGRect.size.width;
float viewheight = mCGRect.size.height;
CGRect rect;
if(centerBool)
rect = CGRectMake((imgwidth-viewwidth)/2, (imgheight-viewheight)/2, viewwidth, viewheight);
else{
if (viewheight < viewwidth) {
if (imgwidth <= imgheight) {
rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
}else {
float width = viewwidth*imgheight/viewheight;
float x = (imgwidth - width)/2 ;
if (x > 0) {
rect = CGRectMake(x, 0, width, imgheight);
}else {
rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
}
}
}else {
if (imgwidth <= imgheight) {
float height = viewheight*imgwidth/viewwidth;
if (height < imgheight) {
rect = CGRectMake(0, 0, imgwidth, height);
}else {
rect = CGRectMake(0, 0, viewwidth*imgheight/viewheight, imgheight);
}
}else {
float width = viewwidth*imgheight/viewheight;
if (width < imgwidth) {
float x = (imgwidth - width)/2 ;
rect = CGRectMake(x, 0, width, imgheight);
}else {
rect = CGRectMake(0, 0, imgwidth, imgheight);
}
}
}
}

CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();

return smallImage;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: