从UIImageView获取上下文 [英] Get context from UIImageView

查看:57
本文介绍了从UIImageView获取上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从存在的 UIImageView 中获取上下文( CGContextRef )吗?

Can I get the context (CGContextRef) from exists UIImageView?

详细信息:我有一个指向xib文件的控制器链接.在IB中,我添加了 UIImageView 并在以下位置引用了它:

Detail: I have a controller link to xib file. In IB I have added a UIImageView and reference its on:

IBOutlet UIImageView *imageView;

现在,在 viewDidLoad 中如何获取 UIImageView 上下文( CGContextRef )?

Now, in viewDidLoad How get the UIImageView context (CGContextRef)?

在此之前,如果我使用:

Before this, If I use:

CGContextRef context = UIGraphicsGetCurrentContext();

输出:

<Error>: CGContextTranslateCTM: invalid context

建议?

推荐答案

如果要使用自己的绘制方法,则应在UIView的drawRect:方法中应用它们.您需要将UIView子类化以创建自己的绘制逻辑:在XCode中,使用File-> New File并选择UIView作为模板,这将为您提供基本的框架.在Interface-Builder中,您可以首先添加一个新视图,然后使用您在检查器窗口中身份"选项卡下的类进行设置.

If you want to use your own drawing methods you should apply them in the drawRect: method of the UIView. You need to subclass UIView to create your own drawing logic: In XCode use File->New File and select UIView as a template which will give you the basic framework. In Interface-Builder you can first add a new view and then set it use your class under Identity Tab in the Inspector window.

然后该方法应以类似以下内容开头,以为您提供对上下文的引用:

The method should then start with something like this to give you a reference to the context:

- (void)drawRect:(CGRect)fr {
    CGContextRef context = UIGraphicsGetCurrentContext();
    ... DRAWING CODE ...
}

第一次显示视图时,将自动调用此方法.如果您的逻辑依赖于某些变量,则应在UIView上调用 setNeedsDisplay ,以使视图重新运行绘图逻辑.

This methods automatically gets called when the view is first displayed. If your logic depends on some variables you should call setNeedsDisplay on the UIView to let the view re-run the drawing logic.

但是,这不适用于 UIImageView (( LINK ):

However, this will not work on UIImageView (LINK):

子类注释

特殊注意事项

Special Considerations

UIImageView类经过优化,可以将其图像绘制到显示器上.UIImageView不会调用drawRect:子类.如果您的子类需要自定义绘图代码,则建议您使用UIView作为基类.

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

这篇关于从UIImageView获取上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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