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

时间:2010-08-31 15:32:55

标签: iphone objective-c cocoa-touch

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

11 个答案:

答案 0 :(得分:24)

前面的代码假设要捕获的视图存在于主屏幕上......它可能不会。

这是否能够始终捕获主窗口的内容? (警告:在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;
}

答案 1 :(得分:17)

您需要创建一个屏幕大小的位图上下文并使用

[self.view.layer renderInContext:c]

将视图复制到其中。完成后,您可以使用

CGBitmapContextCreateImage(c)

从您的上下文创建CGImage。

详细说明:

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

请注意,如果您运行代码以响应点击UIButton,您的图片将显示该按钮被按下。

答案 2 :(得分:7)

技术Q& A QA1703 UIKit应用程序中的屏幕捕获

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

答案 3 :(得分:3)

试试这个......

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

答案 4 :(得分:2)

UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];

更新2011年4月:对于视网膜显示,将第一行更改为:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
    UIGraphicsBeginImageContext(self.window.bounds.size);
}

答案 5 :(得分:1)

// 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;
}

答案 6 :(得分:0)

是的,这是Apple开发者论坛的链接 https://devforums.apple.com/message/149553

答案 7 :(得分:0)

使用此代码截取屏幕截图:

-(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];
}

答案 8 :(得分:0)

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

答案 9 :(得分:0)

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

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

    返回图片; }

答案 10 :(得分:0)

以现代方式:

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

快速

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