como tirar uma captura de canvas do iPhone programaticamente?

É possível no objective C que podemos tirar a canvas da canvas e armazenar esta imagem em UIImage.

Você precisa criar um contexto de bitmap do tamanho da sua canvas e usar

[self.view.layer renderInContext:c] 

para copiar sua visão nele. Feito isso, você pode usar

 CGBitmapContextCreateImage(c) 

para criar um CGImage a partir do seu contexto.

Elaboração:

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

Observe que, se você executar seu código em resposta a um clique em um UIButton, sua imagem mostrará esse botão pressionado.

O código anterior pressupõe que a vista a ser capturada vive na canvas principal … talvez não.

Isso funcionaria para sempre capturar o conteúdo da janela principal? (aviso: compilado em 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; } 

Perguntas e Respostas Técnicas QA1703 Screen Capture in UIKit Applications

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

tente isso …

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

ATUALIZAÇÃO Abril de 2011: para exibição de retina, mude a primeira linha para isso:

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

// 100% de trabalho

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

Sim, aqui está um link para os fóruns de desenvolvedores da Apple https://devforums.apple.com/message/149553

Use este código para tirar a foto:

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