Objective c 如何设置CGContextRef属性而不是使用UIGraphicsGetCurrentContext()呢
我一直在尝试使用UIGraphicsGetCurrentContext和CGContextRef。有人告诉我,多次使用UIGraphicsGetCurrentContext()是不好的,而是使用CGContextRef并引用它 我一直在尝试第二部分的工作,但是我在设置@property和引用它时遇到了问题。有人能给我一个声明和使用的例子吗?尝试过谷歌搜索,但找不到任何相关信息Objective c 如何设置CGContextRef属性而不是使用UIGraphicsGetCurrentContext()呢,objective-c,xcode,core-graphics,Objective C,Xcode,Core Graphics,我一直在尝试使用UIGraphicsGetCurrentContext和CGContextRef。有人告诉我,多次使用UIGraphicsGetCurrentContext()是不好的,而是使用CGContextRef并引用它 我一直在尝试第二部分的工作,但是我在设置@property和引用它时遇到了问题。有人能给我一个声明和使用的例子吗?尝试过谷歌搜索,但找不到任何相关信息 Ta您可能不应该将UIGraphicsGetCurrentContext的返回值存储在属性中。您通常不知道上下文的有效期
Ta您可能不应该将
UIGraphicsGetCurrentContext
的返回值存储在属性中。您通常不知道上下文的有效期有多长,或者上下文的生存期很短。例如,如果从drawRect:
方法调用UIGraphicsGetCurrentContext
,则不知道从drawRect:
返回后该上下文将保留多长时间。如果在调用UIGraphicsBeginImageContextWithOptions
之后调用UIGraphicsGetCurrentContext
,您可能很快就会调用UIGraphicsEndImageContext
。在周围保留对这些上下文的引用是不合适的
如果在同一上下文中调用多个核心图形函数,则需要将上下文存储在局部变量中。例如,下面是我的一个测试项目中的drawRect:
方法:
- (void)drawRect:(CGRect)dirtyRect {
NSLog(@"drawRect:%@", NSStringFromCGRect(dirtyRect));
[self layoutColumnsIfNeeded];
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSaveGState(gc); {
// Flip the Y-axis of the context because that's what CoreText assumes.
CGContextTranslateCTM(gc, 0, self.bounds.size.height);
CGContextScaleCTM(gc, 1, -1);
for (NSUInteger i = 0, l = CFArrayGetCount(_columnFrames); i < l; ++i) {
CTFrameRef frame = CFArrayGetValueAtIndex(_columnFrames, i);
CGPathRef path = CTFrameGetPath(frame);
CGRect frameRect = CGPathGetBoundingBox(path);
if (!CGRectIntersectsRect(frameRect, dirtyRect))
continue;
CTFrameDraw(frame, gc);
}
} CGContextRestoreGState(gc);
}
-(void)drawRect:(CGRect)dirtyRect{
NSLog(@“drawRect:%@),NSStringFromCGRect(dirtyRect));
[需要自行布局];
CGContextRef gc=UIGraphicsGetCurrentContext();
CGContextSaveGState(gc){
//翻转上下文的Y轴,因为这是CoreText假定的。
CGContextTranslateCm(gc,0,self.bounds.size.height);
CGContextScaleCTM(gc,1,-1);
对于(i=0,l=CFArrayGetCount(_columnFrames);i
你可以看到,我正在对上下文做很多事情:我正在保存和恢复图形状态,我正在更改CTM,我正在向其中绘制一些核心文本框。我没有多次调用
UIGraphicsGetCurrentContext
,而是只调用一次,并将结果保存在名为gc
的局部变量中。您可能不应该将UIGraphicsGetCurrentContext
的返回值存储在属性中。您通常不知道上下文的有效期有多长,或者上下文的生存期很短。例如,如果从drawRect:
方法调用UIGraphicsGetCurrentContext
,则不知道从drawRect:
返回后该上下文将保留多长时间。如果在调用UIGraphicsBeginImageContextWithOptions
之后调用UIGraphicsGetCurrentContext
,您可能很快就会调用UIGraphicsEndImageContext
。在周围保留对这些上下文的引用是不合适的
如果在同一上下文中调用多个核心图形函数,则需要将上下文存储在局部变量中。例如,下面是我的一个测试项目中的drawRect:
方法:
- (void)drawRect:(CGRect)dirtyRect {
NSLog(@"drawRect:%@", NSStringFromCGRect(dirtyRect));
[self layoutColumnsIfNeeded];
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSaveGState(gc); {
// Flip the Y-axis of the context because that's what CoreText assumes.
CGContextTranslateCTM(gc, 0, self.bounds.size.height);
CGContextScaleCTM(gc, 1, -1);
for (NSUInteger i = 0, l = CFArrayGetCount(_columnFrames); i < l; ++i) {
CTFrameRef frame = CFArrayGetValueAtIndex(_columnFrames, i);
CGPathRef path = CTFrameGetPath(frame);
CGRect frameRect = CGPathGetBoundingBox(path);
if (!CGRectIntersectsRect(frameRect, dirtyRect))
continue;
CTFrameDraw(frame, gc);
}
} CGContextRestoreGState(gc);
}
-(void)drawRect:(CGRect)dirtyRect{
NSLog(@“drawRect:%@),NSStringFromCGRect(dirtyRect));
[需要自行布局];
CGContextRef gc=UIGraphicsGetCurrentContext();
CGContextSaveGState(gc){
//翻转上下文的Y轴,因为这是CoreText假定的。
CGContextTranslateCm(gc,0,self.bounds.size.height);
CGContextScaleCTM(gc,1,-1);
对于(i=0,l=CFArrayGetCount(_columnFrames);i
你可以看到,我正在对上下文做很多事情:我正在保存和恢复图形状态,我正在更改CTM,我正在向其中绘制一些核心文本框。我没有多次调用
UIGraphicsGetCurrentContext
,而是只调用了一次,并将结果保存在名为gc
的局部变量中,这基本上是推荐的方法。特别是要始终以这种方式动态获取上下文。谢谢,这基本上是推荐的方式。特别是要始终以这种方式动态获取上下文。