How do I take a screenshot of a view which has transform?

纵然是瞬间 提交于 2019-12-06 14:17:53

问题


I'm able to crop a view with this code

- (UIImage *)captureScreenInRect:(CGRect)captureFrame {
    CALayer *layer;
    layer = self.view.layer;
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
}

But I have an imageview zoomed in with transform and it isn't shown to scale.

How do I capture EXACTLY what the user sees on the screen


回答1:


The Stack Overflow question "renderInContext:" and CATransform3D has more info, but the gist is:

QCCompositionLayer, CAOpenGLLayer, and QTMovieLayer layers are not rendered. Additionally, layers that use 3D transforms are not rendered, nor are layers that specify backgroundFilters, filters, compositingFilter, or a mask values.

(from the CALayer docs).

More info is also available in this technical Q&A: http://developer.apple.com/library/ios/#qa/qa1703/_index.html

If your app is not going to the app store you can use the undocumented UIGetScreenImage API:

// Define at top of implementation file
CGImageRef UIGetScreenImage(void);

...

- (void)buttonPressed:(UIButton *)button
{
  // Capture screen here...
  CGImageRef screen = UIGetScreenImage();
  UIImage* image = [UIImage imageWithCGImage:screen];
  CGImageRelease(screen);

  // Save the captured image to photo album
  UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

(from John Muchow)

However, use of this API will make your app not get approved.

I have been unable to find any other workarounds.



来源:https://stackoverflow.com/questions/15574845/how-do-i-take-a-screenshot-of-a-view-which-has-transform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!