2010-08-31 58 views
12

是否可以在目标C中使用屏幕截图并将其存储在UIImage中。如何以编程方式拍摄iPhone的截图?

+1

可能重复http://stackoverflow.com/questions/2214957/how-do-i-take-a-screen-shot-of-a-uiview – 2010-08-31 15:35:16

+1

可能重复[如何采取截图编程](http://stackoverflow.com/questions/2200736/how-to-take-a-screenshot-programmatically) – 2010-09-01 16:03:54

回答

15

你需要创建你的屏幕大小的位图上下文和使用

[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运行你的代码,你的图像将显示,按下按钮。

+0

你可以在这里详细说明吗? Thanx – user12345 2010-09-01 14:27:22

+0

只想提及你错过了释放colorSpaceRef – 2012-05-21 20:07:24

+1

另外,什么是“自我”?窗口转换怎么样?你知道当显示键盘/警报时会发生什么? – Sulthan 2013-07-12 11:34:02

22

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

这个工作是否总是捕获主窗口的内容? (警告:在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; 
} 
+0

我在视图中设置了动画效果,但此代码忽略动画(完成后未删除) – Tibidabo 2012-01-05 05:30:23

+0

谢谢!这是迄今为止我发现的最简单的方法。 – Greg 2012-02-11 02:08:27

+0

非常感谢。 – 2013-02-27 14:55:08

2

试试这个...

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

UPDATE 2011年4月:视网膜显示屏,将第一行到这一点:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
{ 
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale); 
} 
else 
{ 
    UIGraphicsBeginImageContext(self.window.bounds.size); 
} 
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); 
} 
+1

它是100%在我的程序工作,为什么投票下来。?! – 2013-12-30 07:04:38

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

@dhaya的答案正在为我工​​作,但不要忘记更新库中保存图像的“Info.plist”设置。 需要在“Info.plist”文件中添加此代码以将照片图像保存到照片库。的

<key>NSPhotoLibraryAddUsageDescription</key> 
<string>NeedLibrary access for save Images on Album</string>