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


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

2017.03.16 12:18* 字數 52 閱讀 563評論 4喜歡 2

1. 截取屏幕尺寸大小的圖片並保存至相冊

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

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

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

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

2. scrollViewtableView的全部截屏並保存至相冊

- (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:@"保存圖片結果提示"

                                                    message:msg

                                                   delegate:self

                                          cancelButtonTitle:@"確定"

                                          otherButtonTitles:nil];

    [alert show];

}

 

 

+ (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

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 }


鏈接:https://www.jianshu.com/p/343387a9e5c0

iOS應用內截圖代碼

有待改寫工具類?????

2016.06.17 

第一種方法(截全屏幕)

下面這段代碼可以將當前屏幕顯示的內容截圖放置相冊中,需要導入

-(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);

 

}

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

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

 

staticintScreenshotIndex=0; //

 

-(void)ScreenShot{

 

//這里因為我需要全屏接圖所以直接改了,宏定義iPadWithd為1024,iPadHeight為768,

 

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

 

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

 

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

 

UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();

 

UIGraphicsEndImageContext();

 

CGImageRef imageRef =viewImage.CGImage;

 

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

 

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

 

CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

 

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

 

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

 

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);

 

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

 

[self SetPickPath:savedImagePath];

 

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

 

CGImageRelease(imageRefRect);

 

ScreenshotIndex++;

 

}

 

//設置路徑

 

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

 

_ScreenshotsPickPath =PickImage;

 

}

 

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

 

- (NSString *)GetPickPath {

 

return_ScreenshotsPickPath;

 

}

 

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

2016.07.18 09:38* 
 

-(void) screenShot{

    

  UIGraphicsBeginImageContext(self.bounds.size);   //self為需要截屏的UI控件 即通過改變此參數可以截取特定的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);//把圖片保存在本地

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM