6

( UIImageVIew)imageViewが に追加されself.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サブビューが次の方法で回転する場合:

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

再度キャプチャself.viewしても回転は反映されません。サブビューimageViewは回転していないかのようです。

renderInContextで回転したサブビューで動作させる方法はCABasicAnimation?

アップデート:

警告: CALayer/-renderInContext: メソッドは、完全な Core Animation コンポジション モデルを実装していません。以下に示すコードはほとんどの状況を解決できますが、CALayer/-renderInContext: メソッドが正しくレンダリングされないことがあるため、開発者テクニカル サポートに回避策のリクエストを依頼することをお勧めします。

公式の Apple Technical Q&A QA1703 では、開発者テクニカル サポートに回避策のリクエストを依頼することを提案しています。

回避策は既に存在しますか?

4

4 に答える 4

5

whenlayer.presentationLayerの代わりに使用します。layerrenderInContext

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];
于 2012-12-11T04:44:23.867 に答える
2

ドキュメントから:

このメソッドは、レンダリングツリーに追加されたアニメーションを無視して、レイヤーツリーから直接レンダリングします。

プレゼンテーション層から回転を読み取り、グラフィックスコンテキストを回転させる必要があります。

于 2012-12-06T13:13:23.730 に答える
2

プレゼンテーション層で renderInContext を使用できるはずです。これはレイヤー階層をたどらないかもしれませんが。したがって、次のいずれかを実行すると、おそらく最も簡単になります。

  • 影響を受けるすべてのサブレイヤーに回転を適用し、ルートレイヤーをレンダリングします
  • 自分でプレゼンテーション層を手動で構成する再帰的な方法を書く
于 2012-12-10T09:37:52.410 に答える
1

As David said, animations are not rendered by renderInContext. Animations are applied to the presentation layer, which is display only.

The simplest thing to do would be to apply your rotation to your layer inside a CATransaction that disables implicit animations, then capture your layer.

I believe that would work. (However I haven't tried that specific thing, so I'm not positive.)

Alternately, you can set up your animations so that they are not set to stay in force once the animation completes, but instead change the underlying property right before the animation begins. Then when you render the layer, the sublayers should draw in their final positions.

Rendering an "in flight" animation would be much trickier. For that you would need to walk the layer tree, querying each layer's presentation layer, and transferring the settings you are animating to the base layer. That would screw up your layers once the animation is complete however, so you would need to save away the value of each property somehow, set it to it's "in flight" value from the presentation layer, capture your image, then set the property/properties of each layer back, all inside a CATransaction that disables animation.

于 2012-12-07T02:05:36.083 に答える