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

Jab*_*Jab 152 iphone screenshot ios

我想在屏幕上保存到保存的照片库中的图像截图

Den*_*kem 231

考虑检查视网膜显示,请使用以下代码段:

#import <QuartzCore/QuartzCore.h> 

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

[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData) {
    [imageData writeToFile:@"screenshot.png" atomically:YES];
} else {
    NSLog(@"error while taking screenshot");
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这不会捕获某些类型的屏幕内容,例如OpenGL ES图层.华英指出的UIGetScreenImage()确实如此. (13认同)
  • 亲爱的人们,别忘了做#import <QuartzCore/QuartzCore.h> (12认同)
  • 这张图片存放在哪里? (5认同)
  • 在哪里是"foo.png",我找不到png文件.谢谢 (4认同)
  • @ wladimir-palant:键盘怎么样,因为当我使用用户类型的屏幕截图,然后键盘没有捕获,我怎么能在屏幕截图中添加键盘..? (3认同)

Man*_*ann 45

下面的方法也适用于OPENGL对象

//iOS7 or above
- (UIImage *) screenshot {

    CGSize size = CGSizeMake(your_width, your_height);

    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

    CGRect rec = CGRectMake(0, 0, your_width, your_height);
    [_viewController.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
Run Code Online (Sandbox Code Playgroud)

  • `UIGetScreenImage`是一个私有API (5认同)

IOS*_*cks 20

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageData = UIImageJPEGRepresentation(image, 1.0 ); //you can use PNG too
[imageData writeToFile:@"image1.jpeg" atomically:YES];
Run Code Online (Sandbox Code Playgroud)


Ais*_*ina 14

- (UIImage*) getGLScreenshot {
    NSInteger myDataLength = 320 * 480 * 4;

    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // gl renders "upside down" so swap top to bottom into new array.
    // there's gotta be a better way, but this works.
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    for(int y = 0; y <480; y++)
    {
        for(int x = 0; x <320 * 4; x++)
        {
            buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
        }
    }

    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * 320;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}

- (void)saveGLScreenshotToPhotosAlbum {
    UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);  
}
Run Code Online (Sandbox Code Playgroud)

来源.

  • 这似乎也非常愚蠢,他们没有为我们提供更简单的方法. (2认同)

Hua*_*ing 10

看到这篇文章看起来你现在可以使用UIGetScreenImage()了.

  • iOS 4中不允许再次使用此功能. (8认同)
  • 以下是Apple的最新消息:https://devforums.apple.com/message/266836 (7认同)

Vic*_*cky 8

在SWIFT

func captureScreen() -> UIImage
{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);

    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)

    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return image
}
Run Code Online (Sandbox Code Playgroud)


Tul*_*oso 8

从iOS10开始,这会变得更简单一些.UIKit随附UIGraphicsImageRender,可以让你

...完成绘图任务,无需处理颜色深度和图像比例等配置,或管理Core Graphics上下文

Apple Docs - UIGraphicsImageRenderer

所以你现在可以这样做:

let renderer = UIGraphicsImageRenderer(size: someView.bounds.size)

let image = renderer.image(actions: { context in
    someView.drawHierarchy(in: someView.bounds, afterScreenUpdates: true)
})
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,这里的许多答案对我有用.但是当我试图拍摄一张快照时ARSCNView,我只能使用上述方法进行拍摄.虽然值得注意的是,目前ARKit仍处于测试阶段且Xcode处于测试版4


A. *_*dam 6

这将保存屏幕截图并返回屏幕截图.

-(UIImage *)capture{
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(imageView, nil, nil, nil); //if you need to save
    return imageView;
}
Run Code Online (Sandbox Code Playgroud)


tou*_*bun 5

我认为如果您想要全屏(状态栏除外),以下代码段会有所帮助,如果需要,只需将AppDelegate替换为您的应用代理名称即可.

- (UIImage *)captureFullScreen {

    AppDelegate *_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        // for retina-display
        UIGraphicsBeginImageContextWithOptions(_appDelegate.window.bounds.size, NO, [UIScreen mainScreen].scale);
        [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
    } else {
        // non-retina-display
        UIGraphicsBeginImageContext(_bodyView.bounds.size);
        [_appDelegate.window drawViewHierarchyInRect:_appDelegate.window.bounds afterScreenUpdates:NO];
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
Run Code Online (Sandbox Code Playgroud)


Jab*_*Jab 4

我回答这个问题是因为它的关注度很高,而且有很多答案,另外还有 Swift 和 Obj-C。

免责声明 这不是我的代码, 也不是我的答案, 这只是为了帮助来到这里的人们找到快速答案。有原始答案的链接,可以在应得的信用处给予信用! 如果您使用他们的答案,请尊重原始答案+1!


使用QuartzCore

#import <QuartzCore/QuartzCore.h> 

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

[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData) {
    [imageData writeToFile:@"screenshot.png" atomically:YES];
} else {
    NSLog(@"error while taking screenshot");
}
Run Code Online (Sandbox Code Playgroud)

在斯威夫特

func captureScreen() -> UIImage
{

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);

    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)

    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return image
}
Run Code Online (Sandbox Code Playgroud)

注意: 由于编程的性质, 可能需要进行更新,请编辑或告诉我!*此外,如果我未能包含值得包含的答案/方法,请随时告诉我!