UIGraphicsGetCurrentContext与UIGraphicsBeginImageContext / UIGraphicsEndImageContext [英] UIGraphicsGetCurrentContext vs UIGraphicsBeginImageContext/UIGraphicsEndImageContext

查看:804
本文介绍了UIGraphicsGetCurrentContext与UIGraphicsBeginImageContext / UIGraphicsEndImageContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS API的这些部分的新手,这里有一些问题导致我脑海中无限循环

I am new to these parts of iOS API and here are some questions that are causing an infinite loop in my mind


  1. 为什么..BeginImageContext有一个大小,但..GetCurrentContext没有大小?如果..GetCurrentContext没有大小,它在哪里绘制?有什么限制?

  1. Why does ..BeginImageContext have a size but ..GetCurrentContext does not have a size? If ..GetCurrentContext does not have a size, where does it draw? What are the bounds?

为什么他们必须有两个上下文,一个用于图像,一个用于一般图形?图像上下文不是图形上下文吗?分离的原因是什么(我想知道我不知道的事情)

Why did they have to have two contexts, one for image and one for general graphics? Isn't an image context already a graphic context? What was the reason for the separation (I am trying to know what I don't know)


推荐答案

UIGraphicsGetCurrentContext()返回对当前图形上下文的引用。它没有创建一个。这一点很重要,因为如果您在该灯光中查看它,您会发现它不需要大小参数,因为当前上下文只是创建图形上下文的大小。

UIGraphicsGetCurrentContext() returns a reference to the current graphics context. It doesn't create one. This is important to remember because if you view it in that light, you see that it doesn't need a size parameter because the current context is just the size the graphics context was created with.

UIGraphicsBeginImageContext(aSize)用于在UIView的 drawRect:方法之外的UIKit级别创建图形上下文。

UIGraphicsBeginImageContext(aSize) is for creating graphics contexts at the UIKit level outside of UIView's drawRect: method.

您可以在这里使用它们。

Here is where you would use them.

如果你有UIView的子类,你可以覆盖它drawRect:方法如下:

If you had a subclass of UIView you could override its drawRect: method like so:

- (void)drawRect:(CGRect)rect
{
    //the graphics context was created for you by UIView
    //you can now perform your custom drawing below

    //this gets you the current graphic context
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //set the fill color to blue
    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);

    //fill your custom view with a blue rect
    CGContextFillRect(ctx, rect);
}

在这种情况下,您无需创建图形上下文。它是自动为您创建的,允许您在drawRect:方法中执行自定义绘图。

In this case, you didn't need to create the graphics context. It was created for you automatically and allows you to perform your custom drawing in the drawRect: method.

现在,在另一种情况下,您可能希望在外部执行一些自定义绘图drawRect:方法。在这里你可以使用 UIGraphicsBeginImageContext(aSize)

Now, in another situation, you might want to perform some custom drawing outside of the drawRect: method. Here you would use UIGraphicsBeginImageContext(aSize)

你可以这样做:

UIBezierPath *circle = [UIBezierPath
                        bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];  

UIGraphicsBeginImageContext(CGSizeMake(200, 200));

//this gets the graphic context
CGContextRef context = UIGraphicsGetCurrentContext();

//you can stroke and/or fill
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
[circle fill];
[circle stroke];

//now get the image from the context
UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage];

我希望这有助于为您解决问题。此外,您应该使用UIGraphicsBeginImageContextWithOptions(大小,不透明,缩放)。有关使用图形上下文的自定义绘图的进一步说明,请参阅我的答案此处

I hope this helps to clear things up for you. Also, you should be using UIGraphicsBeginImageContextWithOptions(size, opaque, scale). For further explanation of custom drawing with graphics contexts, see my answer here

这篇关于UIGraphicsGetCurrentContext与UIGraphicsBeginImageContext / UIGraphicsEndImageContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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