4

UITextView を取得して、ユーザーがテキストを入力できるようにしてから、コンテンツのコピーを Quartz ビットマップ コンテキストにトリガーしたいと思います。このコピーアクションを実行する方法を知っている人はいますか? drawRect メソッドをオーバーライドして [super drawRect] を呼び出し結果のコンテキストを取得してコピーする必要がありますか? もしそうなら、あるコンテキストから別のコンテキストにコピーするためのサンプルコードへの参照を誰かが持っていますか?

更新: 以下の回答のリンクを読んでから、UIView の内容をビットマップ コンテキストにコピーしようとしましたが、まだ何かが正しくありません。内容を X 軸に沿ってミラーリングします (つまり、上下逆さまにします)。CGContextScaleCTM() を使用してみましたが、効果がないようです。

最初の 4 行から作成された UIImage が、奇妙に回転/反転しない UIImage を適切に作成することを確認したので、後の呼び出しで何か問題があります。

    // copy contents to bitmap context
    UIGraphicsBeginImageContext(mTextView.bounds.size);
    [mTextView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    [self setNeedsDisplay];

    // render the created image to the bitmap context
    CGImageRef cgImage = [image CGImage];

    CGContextScaleCTM(mContext, 1.0, -1.0); // doesn't seem to change result

    CGContextDrawImage(mContext, CGRectMake(
        mTextView.frame.origin.x,
        mTextView.frame.origin.y,
        [image size].width, [image size].height), cgImage); 

助言がありますか?

4

2 に答える 2

1

UIViewのUIImageを取得するために使用したコードは次のとおりです。

@implementation UIView (Sreenshot)

    - (UIImage *)screenshot{
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);

        /* iOS 7 */
        BOOL visible = !self.hidden && self.superview;
        CGFloat alpha = self.alpha;
        BOOL animating = self.layer.animationKeys != nil;
        BOOL success = YES;
        if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]){
            //only works when visible
            if (!animating && alpha == 1 && visible) {
                success = [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
            }else{
                self.alpha = 1;
                success = [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
                self.alpha = alpha;
            }
        }
        if(!success){ /* iOS 6 */
            self.alpha = 1;
            [self.layer renderInContext:UIGraphicsGetCurrentContext()];
            self.alpha = alpha;
        }

        UIImage* img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return img;
    }

@end
于 2015-04-09T06:23:05.103 に答える
0

iOS 7 以降で使用できます。

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates 
于 2016-04-15T06:50:43.840 に答える