objective-c - renderInContext 不捕获旋转的 subview

标签 objective-c ios uikit core-animation

UIImageVIew (imageView) 被添加到 self.view 中,完美地捕捉到相册的 View :

 CGSize size = CGSizeMake(self.view.frame.size.height, self.view.frame.size.width);
 UIGraphicsBeginImageContext(size);
 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

但是,如果 imageView subview 旋转了:

 [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

再次捕获 self.view 不会反射(reflect)旋转。 subview imageView 就好像没有旋转一样。

如何使 renderInContextCABasicAnimation 旋转的 subview 一起工作?

更新:

Warning: The CALayer/-renderInContext: method doesn't implement the full Core Animation composition model. The code provided below will be able to resolve most of the situations, but there are things that the CALayer/-renderInContext: method doesn't render correctly, so you may wish to contact Developer Technical Support for workaround requests.

Apple 官方技术问答 QA1703建议联系开发人员技术支持以获取解决方法请求。

是否已经存在解决方法?

最佳答案

renderInContext 时使用 layer.presentationLayer 而不是 layer

这里是UIView截屏的,UIView+Screenshot.h:

 //
 //  UIView+Screenshot.h
 //
 //  Created by Horace Ho on 2012/12/11.
 //  Copyright (c) 2012 Horace Ho. All rights reserved.
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to deal
 // in the Software without restriction, including without limitation the rights
 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 // copies of the Software, and to permit persons to whom the Software is
 // furnished to do so, subject to the following conditions:
 //
 // The above copyright notice and this permission notice shall be included in
 // all copies or substantial portions of the Software.
 //
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.

 @interface UIView (HHScreenShot)
 - (UIImage *)screenshot:(UIDeviceOrientation)orientation isOpaque:(BOOL)isOpaque usePresentationLayer:(BOOL)usePresentationLayer;
 @end

 @implementation UIView (HHScreenShot)

 - (UIImage *)screenshot:(UIDeviceOrientation)orientation isOpaque:(BOOL)isOpaque usePresentationLayer:(BOOL)usePresentationLayer
 {
     CGSize size;

     if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
         size = CGSizeMake(self.frame.size.width, self.frame.size.height);
     } else {
         size = CGSizeMake(self.frame.size.height, self.frame.size.width);
     }

     UIGraphicsBeginImageContextWithOptions(size, isOpaque, 0.0);

     if (usePresentationLayer) {
         [self.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
     } else {
         [self.layer renderInContext:UIGraphicsGetCurrentContext()];
     }

     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

     return image;
 }

 @end

使用:

 UIImage *image = [self.view screenshot:UIDeviceOrientationPortrait
                               isOpaque:YES 
                   usePresentationLayer:YES];

关于objective-c - renderInContext 不捕获旋转的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13743755/

相关文章:

ios - 为每个 UITableViewCell 创建一个按钮

iphone - 与直接mainBundle引用相比,checkAndCreateDatabase方法的优势

ios - AppDelegate:在解包可选值时意外发现 nil

ios - 通过 Relay Server 连接 iOS App SAP-SUP

ios - 使用 UIDocumentPickerViewController 导入文档警告 "fileprovider plugin was invalidated"

iphone - iPad : how to change background color for Ipad app?

objective-c - viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 指南

ios - 为什么 input[type=range] 在 CSS3 转换后不起作用?

objective-c - 在 Subview 或 ViewController 中处理触摸事件逻辑

ios - 没有为 UITableViewCell 对象调用 awakeFromNib