問題描述
iPhone:如何在使用 3d 圖層轉換時以編程方式拍攝屏幕截圖 (iPhone: how to take screen shots programmatically when using 3d layer transformations)
我正在嘗試以編程方式將當前視圖的圖像保存到 UIImage。當前視圖包含一個背景,以及許多對圖層應用了 3d 轉換的 UIImageView,例如
CATransform3D t = CATransform3DIdentity;
t = CATransform3DRotate(t, rotationX, 1.0, 0, 0);
t = CATransform3DRotate(t, rotationY, 0, 1.0, 0);
object.layer.transform = t;
我目前正在使用以下代碼抓取當前視圖的屏幕截圖:
UIGraphicsBeginImageContext(background.bounds.size);
[background.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但是,對像上的 3d 變換會丟失,例如,對象會出現在保存的圖像中,就好像沒有應用 3d 變換一樣。有沒有辦法以編程方式抓取包含 3d 轉換的屏幕截圖?
UIGetScreenImage() 不是一個選項,因為它是一個私有 API,而且這個應用程序將進入應用程序商店。
## 參考解法 #### 方法 1:
I don't think it can be done at the moment, as renderInContext does not render Core Animation transforms and UIGetScreenImage() is not a public API as you well point out. The same question was asked already on StackOverflow:
1) How do I create/render a UIImage from a 3D transformed UIImageView?
If that feature is key to your application, I'd look into using an OpenGL view and capturing the color buffer (which can be rendered offscreen if you don't want to display it).
(by Andrew、Julio Gorgé)