ios截屏代码[转]

http://www.cnblogs.com/chenxiangxi/p/3547974.html

这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员很有帮助

http://www.2cto.com/kf/201310/250228.html

我将其改为以下代码:

 1 #pragma mark -=====自定义截屏位置大小的逻辑代码=====-
 2 static int ScreenshotIndex=0; //这里的逻辑直接采用上面博主的逻辑了
 3 -(void)ScreenShot{
 4     //这里因为我需要全屏接图所以直接改了,宏定义iPadWithd为1024,iPadHeight为768,
 5 //    UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);     //设置截屏大小
 6     UIGraphicsBeginImageContextWithOptions(CGSizeMake(iPadWidth, iPadHeight), YES, 0);     //设置截屏大小
 7     [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
 8     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
 9     UIGraphicsEndImageContext();
10     CGImageRef imageRef = viewImage.CGImage;
11 //    CGRect rect = CGRectMake(166, 211, 426, 320);//这里可以设置想要截图的区域
12     CGRect rect = CGRectMake(0, 0, iPadWidth, iPadHeight);//这里可以设置想要截图的区域
13     CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
14     UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
15     UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库
16     NSData *imageViewData = UIImagePNGRepresentation(sendImage);
17
18     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
19     NSString *documentsDirectory = [paths objectAtIndex:0];
20     NSString *pictureName= [NSString stringWithFormat:@"screenShow_%d.png",ScreenshotIndex];
21     NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];
22     NSLog(@"截屏路径打印: %@", savedImagePath);
23     //这里我将路径设置为一个全局String,这里做的不好,我自己是为了用而已,希望大家别这么写
24     [self SetPickPath:savedImagePath];
25
26     [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录
27     CGImageRelease(imageRefRect);
28     ScreenshotIndex++;
29 }
30 //设置路径
31 - (void)SetPickPath:(NSString *)PickImage {
32     _ScreenshotsPickPath = PickImage;
33 }
34 //获取路径<这里我就直接用于邮件推送的代码中去了,能达到效果,但肯定有更好的写法>
35 - (NSString *)GetPickPath {
36     return _ScreenshotsPickPath;
37 }

时间: 2024-11-08 09:43:05

ios截屏代码[转]的相关文章

iOS截屏代码

/** *截图功能 */ -(void)screenShot{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); //设置截屏大小 [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGrap

IOS截屏

最近,在找IOS截屏的方法.找到一个,现在记录下来.跟我想的差不多,还是,进入操作(初始化),操作(复制当前屏幕,保存),退出.最小的操作结构. 1 . 先指定图像的大小 UIGraphicsBeginImageContext(view.frame.size); 2. 在指定的区域绘制图像 [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO]; 3. 获取图像上下文 UIImage *image = UIGraphics

IOS截屏,View截图的基本方法

IOS截屏的方法网上有很多,以下是我个人认为比较好的一个,我稍微改了一点 来源:SDScreenshotCapture #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) UIImage *getImageWithFullScreenshot(vo

ios 截屏

把当前屏幕作为获取成为图片 - (UIImage *)rn_screenshot {    UIGraphicsBeginImageContext(self.bounds.size);    [self.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext(

c# 截屏代码

//方法一 public static void GetScreen1() { //截取屏幕内容 Size screen = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Bitmap memoryImage = new Bitmap(screen.Width, screen.Height); Graphics memoryGraphics = Graphics.FromImage

ios摇一摇截屏代码

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1.添加一个视图 UIView *greenView=[[UIView alloc]init]; greenView.frame=CGRectMake(100, 100, 100, 100); greenView.b

ios 截屏(代码)

//1.首先在storyboard中拖一些控件,包括UIButton控件,将UIButton控件拖线到控制器中(方法.CutImage) //2.在CutImage方法中调用NSTimer方法 - (IBAction)CutImage:(UIButton *)sender { NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 targer:self  selector:@selector(clipImage) userInf

Android新姿势:截屏代码整理

今天做项目要用到android截屏功能,一开始我还庆幸看过一些博客的文章,自信能轻松解决...- - 结果坑了一天才搞了个差不多的交差...哎! 关于android截屏的代码,大致有3种方法,有兴趣的看下去吧. 方法一: 网上看了很多文章,大多用的是这样的方法,直接把一个View转换成Bitmap,然后保存到sd卡. /** * 根据view来生成bitmap图片,可用于截图功能 */ public static Bitmap getViewBitmap(View v) { v.clearFoc

iOS 截屏以及相关扩展(UIImage的绘制和渲染)

1.截取当前屏幕 CGSize windowSize = behandView.bounds.size; UIGraphicsBeginImageContextWithOptions(windowSize, YES, 2.0); CGContextRef context = UIGraphicsGetCurrentContext(); [behandView.window.layer renderInContext:context]; UIImage *snapshot = UIGraphics