简体   繁体   English

如何以编程方式拍摄iPhone的屏幕截图?

[英]how to take a screenshot of the iPhone programmatically?

在目标C中,我们是否可以拍摄屏幕截图并将此图像存储在UIImage中。

The previous code assumes that the view to be captured lives on the main screen...it might not. 先前的代码假定要捕获的视图位于主屏幕上……可能不会。

Would this work to always capture the content of the main window? 这样是否可以始终捕获主窗口的内容? (warning: compiled in StackOverflow) (警告:在StackOverflow中编译)


- (UIImage *) captureScreen {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect rect = [keyWindow bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

You need to create a bitmap context of the size of your screen and use 您需要创建一个屏幕大小的位图上下文并使用

[self.view.layer renderInContext:c]

to copy your view in it. 在其中复制您的视图。 Once this is done, you can use 完成后,您可以使用

CGBitmapContextCreateImage(c)

to create a CGImage from your context. 根据您的上下文创建CGImage。

Elaboration : 详细说明:

CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(ctx, 0.0, screenSize.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

[(CALayer*)self.view.layer renderInContext:ctx];

CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CGContextRelease(ctx);  
[UIImageJPEGRepresentation(image, 1.0) writeToFile:@"screen.jpg" atomically:NO];

Note that if you run your code in response to a click on a UIButton, your image will shows that button pressed. 请注意,如果您响应单击UIButton来运行代码,则图像将显示该按钮被按下。

Technical Q&A QA1703 Screen Capture in UIKit Applications UIKit应用程序中的技术问答QA1703屏幕截图

http://developer.apple.com/iphone/library/qa/qa2010/qa1703.html http://developer.apple.com/iphone/library/qa/qa2010/qa1703.html

try this... 尝试这个...

- (UIImage*)captureView:(UIView *)view 
{
    CGRect rect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

- (void)saveScreenshotToPhotosAlbum:(UIView *)view 
{
    UIImageWriteToSavedPhotosAlbum([self captureView:self.view], nil, nil,nil);
}
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];

UPDATE April 2011: for retina display, change the first line into this: 2011年4月更新:对于视网膜显示,将第一行更改为:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
    UIGraphicsBeginImageContext(self.window.bounds.size);
}
- (void)SnapShot {
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(self.view.bounds.size);
    }
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

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

    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:@"snapshot.png" options:NSDataWritingWithoutOverwriting error:Nil];
    [data writeToFile:@"snapshot.png" atomically:YES];

    UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], nil, nil, nil);
}

// 100% Work // 100%工作

- (UIImage *)screenshot
{                   
  UIGraphicsBeginImageContextWithOptions(self.main_uiview.bounds.size, NO, 2.0f);
  [self.main_uiview drawViewHierarchyInRect:_main_uiview.bounds afterScreenUpdates:YES];

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

  return image;
}

Use this code to take the screenshot: 使用以下代码拍摄屏幕截图:

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
UIGraphicsBeginImageContext(self.webView.bounds.size);

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

    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


    NSString *str1=[NSString stringWithFormat:@"%d",myInt];

    NSString *pngFilePath = [NSString stringWithFormat:@"%@/page_%@.png",docDir,str1]; // any name u want for image
    NSLog(@"%@",pngFilePath);
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(screenshotImage)]; 
    [data1 writeToFile:pngFilePath atomically:YES];
}

是的,这里是指向苹果开发者论坛的链接https://devforums.apple.com/message/149553

  • (UIImage *)screenshot { UIGraphicsBeginImageContextWithOptions(self.main_uiview.bounds.size, NO, 2.0f); (UIImage *)屏幕截图{UIGraphicsBeginImageContextWithOptions(self.main_uiview.bounds.size,NO,2.0f); [self.main_uiview drawViewHierarchyInRect:_main_uiview.bounds afterScreenUpdates:YES]; [self.main_uiview drawViewHierarchyInRect:_main_uiview.bounds afterScreenUpdates:YES];

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

    return image; 返回图像; } }

In modern way: 用现代的方式:

Obj-C 对象

@interface UIView (Snapshot)
- (UIImage * _Nullable)snapshot;
@end

@implementation UIView (Snapshot)

- (UIImage * _Nullable)snapshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, UIScreen.mainScreen.scale);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

Swift 迅速

extension UIView {
    func snapshot() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
        drawHierarchy(in: bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM