iOS截屏screenshot(屏幕截屏及scrollView或tableView的全部截屏)

1、1. 截取屏幕尺寸大小的圖片并保存至相冊(cè)

保存至相冊(cè)只需將方法saveImage中的代碼替換即可

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0.0);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

2 2. scrollView或tableView的全部截屏并保存至相冊(cè)

- (void)saveImage {

    UIImage* viewImage = nil;

    UITableView *scrollView = self.tableView;

    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0);

    {

        CGPoint savedContentOffset = scrollView.contentOffset;

        CGRect savedFrame = scrollView.frame;

        

        scrollView.contentOffset = CGPointZero;

        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

        

        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];

        viewImage = UIGraphicsGetImageFromCurrentImageContext();

        

        scrollView.contentOffset = savedContentOffset;

        scrollView.frame = savedFrame;

    }

    UIGraphicsEndImageContext();

    

    [self saveImageToPhotos:viewImage];

}

- (void)saveImageToPhotos:(UIImage*)savedImage {

    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

}

 

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {

    NSString *msg = nil ;

    if(error != NULL){

        msg = @"保存圖片失敗" ;

    }else{

        msg = @"保存圖片成功" ;

    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存圖片結(jié)果提示"

                                                    message:msg

                                                   delegate:self

                                          cancelButtonTitle:@"確定"

                                          otherButtonTitles:nil];

    [alert show];

}

screenshot

+ (UIImage *)screenshot
{
    CGSize imageSize = CGSizeZero;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = [UIScreen mainScreen].bounds.size;
    } else {
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    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);
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        } else {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }

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

swift screenshot

class func screenshot() -> UIImage {
    var imageSize = CGSizeZero

    let orientation = UIApplication.sharedApplication().statusBarOrientation
    if UIInterfaceOrientationIsPortrait(orientation) {
        imageSize = UIScreen.mainScreen().bounds.size
    } else {
        imageSize = CGSize(width: UIScreen.mainScreen().bounds.size.height, height: UIScreen.mainScreen().bounds.size.width)
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
    let context = UIGraphicsGetCurrentContext()
    for 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)
        if orientation == .LandscapeLeft {
            CGContextRotateCTM(context, CGFloat(M_PI_2))
            CGContextTranslateCTM(context, 0, -imageSize.width)
        } else if orientation == .LandscapeRight {
            CGContextRotateCTM(context, -CGFloat(M_PI_2))
            CGContextTranslateCTM(context, -imageSize.height, 0)
        } else if orientation == .PortraitUpsideDown {
            CGContextRotateCTM(context, CGFloat(M_PI))
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height)
        }
        if window.respondsToSelector("drawViewHierarchyInRect:afterScreenUpdates:") {
            window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
        } else if let context = context {
            window.layer.renderInContext(context)
        }
        CGContextRestoreGState(context)
    }

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

iOS應(yīng)用內(nèi)截圖代碼

有待改寫(xiě)工具類?????

第一種方法(截全屏幕)

下面這段代碼可以將當(dāng)前屏幕顯示的內(nèi)容截圖放置相冊(cè)中,需要導(dǎo)入

-(void)viewDidAppear:(BOOL)animated

 

{

 

[superviewDidAppear:animated];

 

self.view.backgroundColor= [UIColorgreenColor];

 

UIWindow*screenWindow = [[UIApplicationsharedApplication]keyWindow];

 

UIGraphicsBeginImageContext(screenWindow.frame.size);

 

[screenWindow.layerrenderInContext:UIGraphicsGetCurrentContext()];

 

UIImage* viewImage =UIGraphicsGetImageFromCurrentImageContext();

 

UIGraphicsEndImageContext();

 

UIImageWriteToSavedPhotosAlbum(viewImage,nil,nil,nil);

 

}

第二種方法(自定義截圖區(qū)域)

pragmamark -=====自定義截屏位置大小的邏輯代碼=====-

staticintScreenshotIndex=0; //

-(void)ScreenShot{

 

//這里因?yàn)槲倚枰两訄D所以直接改了,宏定義iPadWithd為1024,iPadHeight為768,

 

//UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);//設(shè)置截屏大小

 

UIGraphicsBeginImageContextWithOptions(CGSizeMake(iPadWidth, iPadHeight), YES,0);//設(shè)置截屏大小

 

[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];

 

UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();

 

UIGraphicsEndImageContext();

 

CGImageRef imageRef =viewImage.CGImage;

 

//CGRect rect = CGRectMake(166, 211, 426, 320);//這里可以設(shè)置想要截圖的區(qū)域

 

CGRect rect = CGRectMake(0,0, iPadWidth, iPadHeight);//這里可以設(shè)置想要截圖的區(qū)域

 

CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

 

UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];

 

UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存圖片到照片庫(kù)

 

NSData *imageViewData =UIImagePNGRepresentation(sendImage);

 

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 

NSString *documentsDirectory = [paths objectAtIndex:0];

 

NSString *pictureName= [NSString stringWithFormat:@"screenShow_%d.png",ScreenshotIndex];

 

NSString *savedImagePath =[documentsDirectory stringByAppendingPathComponent:pictureName];

 

NSLog(@"截屏路徑打印: %@", savedImagePath);

 

//這里我將路徑設(shè)置為一個(gè)全局String,這里做的不好,我自己是為了用而已,希望大家別這么寫(xiě)

 

[self SetPickPath:savedImagePath];

 

[imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目錄

 

CGImageRelease(imageRefRect);

 

ScreenshotIndex++;

 

}

//設(shè)置路徑

- (void)SetPickPath:(NSString *)PickImage {

 

_ScreenshotsPickPath =PickImage;

 

}


//獲取路徑<這里我就直接用于郵件推送的代碼中去了,能達(dá)到效果,但肯定有更好的寫(xiě)法>

- (NSString *)GetPickPath {
return_ScreenshotsPickPath;

}

IOS獲取設(shè)備屏幕代碼(截屏)

-(void) screenShot{

    

  UIGraphicsBeginImageContext(self.bounds.size);   //self為需要截屏的UI控件 即通過(guò)改變此參數(shù)可以截取特定的UI控件 

  [self.layer renderInContext:UIGraphicsGetCurrentContext()];    

  UIImage *image= UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();    

  NSLog(@"image:%@",image); //至此已拿到image

 

  UIImageView *imaView = [[UIImageView alloc] initWithImage:image];   

  imaView.frame = CGRectMake(0, 700, 500, 500);    

  [self addSubview:imaView];    

    

  UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);//把圖片保存在本地

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容