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

IOS截屏的方法网上有很多,以下是我个人认为比较好的一个,我稍微改了一点

来源:SDScreenshotCapture

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
UIImage *getImageWithFullScreenshot(void)
{
    // Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35

    BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    CGSize imageSize = CGSizeZero;
    if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation)
        imageSize = [UIScreen mainScreen].bounds.size;
    else
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();

    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);

        // Correct for the screen orientation
        if(!ignoreOrientation)
        {
            if(orientation == UIInterfaceOrientationLandscapeLeft)
            {
                CGContextRotateCTM(context, (CGFloat)M_PI_2);
                CGContextTranslateCTM(context, 0, -imageSize.width);
            }
            else if(orientation == UIInterfaceOrientationLandscapeRight)
            {
                CGContextRotateCTM(context, (CGFloat)-M_PI_2);
                CGContextTranslateCTM(context, -imageSize.height, 0);
            }
            else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
            {
                CGContextRotateCTM(context, (CGFloat)M_PI);
                CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
            }
        }

        if([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO];
        else
            [window.layer renderInContext:UIGraphicsGetCurrentContext()];

        CGContextRestoreGState(context);
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

以上是全屏截图,下面修改部分区域截图,以下代码是UIWindow的Category

h文件

#import <UIKit/UIKit.h>

@interface UIWindow (Category)

- (UIImage *)screenshot;
- (UIImage *)screenshotWithRect:(CGRect)rect;

@end

m文件

#import "UIWindow+Category.h"

@implementation UIWindow (Category)

- (UIImage *)screenshot
{
    return [self screenshotWithRect:self.bounds];
}

- (UIImage *)screenshotWithRect:(CGRect)rect
{
    // Source (Under MIT License): https://github.com/shinydevelopment/SDScreenshotCapture/blob/master/SDScreenshotCapture/SDScreenshotCapture.m#L35

    BOOL ignoreOrientation = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0");

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    CGSize imageSize = CGSizeZero;
    CGFloat width = rect.size.width, height = rect.size.height;
    CGFloat x = rect.origin.x, y = rect.origin.y;

//    imageSize = CGSizeMake(width, height);
//    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
    if (UIInterfaceOrientationIsPortrait(orientation) || ignoreOrientation)
    {
        //imageSize = [UIScreen mainScreen].bounds.size;
        imageSize = CGSizeMake(width, height);
        x = rect.origin.x, y = rect.origin.y;
    }
    else
    {
        //imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
        imageSize = CGSizeMake(height, width);
        x = rect.origin.y, y = rect.origin.x;
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, self.center.x, self.center.y);
    CGContextConcatCTM(context, self.transform);
    CGContextTranslateCTM(context, -self.bounds.size.width * self.layer.anchorPoint.x, -self.bounds.size.height * self.layer.anchorPoint.y);

    // Correct for the screen orientation
    if(!ignoreOrientation)
    {
        if(orientation == UIInterfaceOrientationLandscapeLeft)
        {
            CGContextRotateCTM(context, (CGFloat)M_PI_2);
            CGContextTranslateCTM(context, 0, -self.bounds.size.height);
            CGContextTranslateCTM(context, -x, y);
        }
        else if(orientation == UIInterfaceOrientationLandscapeRight)
        {
            CGContextRotateCTM(context, (CGFloat)-M_PI_2);
            CGContextTranslateCTM(context, -self.bounds.size.width, 0);
            CGContextTranslateCTM(context, x, -y);
        }
        else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
        {
            CGContextRotateCTM(context, (CGFloat)M_PI);
            CGContextTranslateCTM(context, -self.bounds.size.height, -self.bounds.size.width);
            CGContextTranslateCTM(context, x, y);
        }
        else
        {
            CGContextTranslateCTM(context, -x, -y);
        }
    }
    else
    {
        CGContextTranslateCTM(context, -x, -y);
    }

    //[self layoutIfNeeded];

    if([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
    else
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    CGContextRestoreGState(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end

此代码在旋转后,裁剪区域是相对左上角为原点旋转的,一般使用不到旋转情况

View截图

h文件

@interface UIView (Screenshot)
- (UIImage *)screenshot;
- (UIImage *)screenshotWithRect:(CGRect)rect;
@end

m文件

@implementation UIView (Screenshot)

- (UIImage *)screenshot
{
    return [self screenshotWithRect:self.bounds];
}

- (UIImage *)screenshotWithRect:(CGRect)rect;
{
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);

    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == NULL)
    {
        return nil;
    }
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);

    //[self layoutIfNeeded];

    if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
    {
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
    }
    else
    {
        [self.layer renderInContext:context];
    }

    CGContextRestoreGState(context);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

//    NSData *imageData = UIImageJPEGRepresentation(image, 1); // convert to jpeg
//    image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];

    return image;
}
时间: 2024-08-21 12:09:34

IOS截屏,View截图的基本方法的相关文章

IOS截屏

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

ios 截屏

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

iOS截屏并修改截图然后分享的功能实现

一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScreenshotNotification, 这个通知会告知注册了此通知的对象已经发生了截屏事件,然后我们就可以在这个事件中实现自己的逻辑 1.注册通知 - (void)viewDidLoad { [super viewDidLoad]; //注册用户的截屏操作通知 [[NSNotificationCe

ios截屏使用的方法

- (UIImage*)getimage{//截屏使用的方法CGSize imageSize = [[UIScreen mainScreen] bounds].size;if (NULL != UIGraphicsBeginImageContextWithOptions) {UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);} else {UIGraphicsBeginImageContext(imageSize);}CGConte

iOS 截屏方法

static int ScreenshotIndex=0; //判断是否是retina屏 if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){ // 开启图像上下文 UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale); } else { // 开启图像上下文 UIGra

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; //这里的逻辑直接采用上面博主的逻辑

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

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

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

iOS截屏代码

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