代码笔记对UIView进行截图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码笔记对UIView进行截图相关的知识,希望对你有一定的参考价值。
一,效果图。
二,工程图。
三,代码。
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//UIView
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
view.backgroundColor=[UIColor redColor];
[self.view addSubview:view];
//在UIImageView中显示截取的图片
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 200, 100)];
imageView.image=[self screenShotView:view];
[self.view addSubview:imageView];
}
#pragma -mark -functions
// 对指定视图进行截图
- (UIImage *)screenShotView:(UIView *)view
{
UIImage *imageRet = nil;
if (view)
{
if(UIGraphicsBeginImageContextWithOptions)
{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
}
else
{
UIGraphicsBeginImageContext(view.frame.size);
}
//获取图像
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
imageRet = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}else{
}
return imageRet;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
以上是关于代码笔记对UIView进行截图的主要内容,如果未能解决你的问题,请参考以下文章