将一个UIView的绘制内容复制到另一个 [英] Copying the drawn contents of one UIView to another

查看:387
本文介绍了将一个UIView的绘制内容复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个UITextView,并允许用户输入文本,然后触发内容的复制到石英位图上下文。有谁知道我如何可以执行此复制操作?我应该重写drawRect方法并调用[super drawRect]和然后获取生成的上下文并复制它吗?如果是这样,任何人都有参考示例代码从一个上下文复制到另一个上下文?

I'd like to take a UITextView and allow the user to enter text into it and then trigger a copy of the contents onto a quartz bitmap context. Does anyone know how I can perform this copy action? Should I override the drawRect method and call [super drawRect] and then take the resulting context and copy it? If so, does anyone have any reference to sample code to copy from one context to another?

更新:从阅读下面的答案中的链接,尝试将我的UIView内容复制到位图上下文中,但是一些仍然不正确。我得到我的内容镜像横跨X轴(即倒置)。我尝试使用CGContextScaleCTM(),但似乎没有效果。

Update: from reading the link in the answer below, I put together this much to attempt to copy my UIView contents into a bitmap context, but something is still not right. I get my contents mirrored across the X axis (i.e. upside down). I tried using CGContextScaleCTM() but that seems to have no effect.

我已经验证,从前四行创建的UIImage正确创建一个UIImage, t奇怪的旋转/翻转,所以有一些我做错了后来的调用。

I've verified that the created UIImage from the first four lines do properly create a UIImage that isn't strangely rotated/flipped, so there is something I'm doing wrong with the later calls.

    // 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); 

有任何建议吗?

推荐答案

下面是我用来获取UIView的UIImage的代码:

Here is the code I used to get a UIImage of UIView:

@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

这篇关于将一个UIView的绘制内容复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆