• ios截屏代码[转]


    这位博主的连接中将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 }

     重要备注: 上面代码只适用于截取普通视图如:UIScrollView、UIView、UIImageView等等,不适用于OpenGL ES、相机内容等,若需要实现对三维模型或者拍照一类截屏功能,还需要另外寻找截取OpenGL渲染区部分的截屏代码或者截取相机内容的截屏代码。

  • 相关阅读:
    day7 面向对象 静态方法 类方法 属性方法 类的特殊成员方法 元类 反射 异常处理
    day6 面向对象 封装 继承 多态 类与实例在内存中的关系 经典类和新式类
    day5 time datetime random os sys shutil json pickle shelve xml configparser hashlib subprocess logging re正则 python计算器
    kafka常用操作命令
    linux基础
    django学习1——初识web应用程序
    mysql数据库(三)——pymysql模块
    mysql数据库(二)——表的查询
    mysql数据库(一)
    Python常用模块——re模块
  • 原文地址:https://www.cnblogs.com/chenxiangxi/p/3547974.html
Copyright © 2020-2023  润新知