gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:49:37 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com