cocoa – 将一个UIView的绘制内容复制到另一个UIView

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

更新:从阅读下面答案中的链接,我把这么多组合在一起试图将我的UIView内容复制到位图上下文中,但有些事情仍然不对.我将我的内容镜像到X轴(即颠倒).我尝试使用CGContextScaleCTM(),但似乎没有效果.

我已经验证了前四行中创建的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);

有什么建议?

这是我用来获取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
相关文章
相关标签/搜索