Tag: cgcontext

如何在Swift中用draw(_ rect:CGRect)绘制一个不同的混合模式的UILabel

所以我有一个UILabel正在绘制的梯度图像(这是一个UIImageView )的顶部。 它看起来像这样: 我试图在UILabel的draw(_ rect: CGRect)函数中更改graphics上下文的blendMode ,以便绘制标签,但是使用softLight混合模式与背景混合。 这是我想要的样子: 这里是我在draw(_ rect: CGRect)函数中的代码: override func draw(_ rect: CGRect) { // Drawing code if let context = UIGraphicsGetCurrentContext() { context.setBlendMode(.softLight) self.textColor.set() self.drawText(in: rect) } } 此代码只呈现顶部图片而不是混合版本。 这并不是我所尝试过的,我尝试了很多其他的东西,但是我不明白CGContext和draw函数。 在SO上的大多数答案要么在旧的Objective-C中,并且被弃用/破坏或者太快和太复杂(例如使用Metal)。 我只想在上下文中绘制文本,并将上下文的混合模式设置为柔和光线。 必须有一个简单的方法! 任何帮助如何解决这个将不胜感激。 谢谢!

用CGContextSetLineDash绘制一条虚线

我试图用CGContextSetLineDash画一条虚线。 这是我的代码: float dashPhase = 0.0; float dashLengths[] = {30, 30}; CGContextSetLineDash(context, dashPhase, dashLengths, 20.0); self.previousPoint2 = self.previousPoint1; self.previousPoint1 = previous; self.currentPoint = current; self.mid1 = [self pointBetween:self.previousPoint1 andPoint:self.previousPoint2]; self.mid2 = [self pointBetween:self.currentPoint andPoint:self.previousPoint1]; UIBezierPath* newPath = [UIBezierPath bezierPath]; [newPath moveToPoint:self.mid1]; [newPath addLineToPoint:self.mid2]; [newPath setLineWidth:self.brushSize]; 但是,如果我画慢,他们的虚线不会出现(见下图),但如果我快速绘制,它们确实出现(见下图)。 为什么发生这种情况?

drawInRect:丢失图像分辨率的质量

我正在尝试使用以下代码擦除图像 CGColorRef strokeColor = [UIColor whiteColor].CGColor; UIGraphicsBeginImageContext(imgForeground.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); [imgForeground.image drawInRect:CGRectMake(0, 0, imgForeground.frame.size.width, imgForeground.frame.size.height)]; CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineWidth(context, 10); CGContextSetStrokeColorWithColor(context, strokeColor); CGContextSetBlendMode(context, kCGBlendModeClear); CGContextBeginPath(context); CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); CGContextStrokePath(context); imgForeground.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 但我只是发现图像失去其解决由于drawInRect 。

CGContextSaveGState vs UIGraphicsPushContext

有两个drawRect方法: – (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); // do drawing here CGContextRestoreGState(context); } 和 – (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); UIGraphicsPushContext(context); // do drawing here UIGraphicsPopContext(); } UIGraphicsPushContext / UIGraphicsPopContext来自UIKit,而CGContextSaveGState / CGContextRestoreGState来自CoreGraphics 。 问题 :这些方法有什么区别? 哪一个更好用? 是否有一些比其他方法更好的certificate方法,反之亦然?

画一个简单的圆形uiimage

我试图做一个简单的蓝色圆圈的20×20 UIImage。 我尝试使用这个函数,但结果是在一个黑色的方形蓝圈。 如何去除圆周上的黑色方块? function: + (UIImage *)blueCircle { static UIImage *blueCircle = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), YES, 0.0f); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); CGRect rect = CGRectMake(0, 0, 20, 20); CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor); CGContextFillEllipseInRect(ctx, rect); CGContextRestoreGState(ctx); blueCircle = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); }); return blueCircle; } 实际结果:

如何使用NSString的sizeWithFont和drawInRect来锻炼多lessstring来绘制

我在iOS中使用CGContext绘制多个图像的“页面”。 我在我的应用程序中广泛使用了sizeWithFont和drawInRect组合。 我需要做的是将多个文本分成多个页面。 我可以确定它是否需要另一个页面,但是我怎么知道在哪里切割? 我必须做一个丑陋的循环逐字检查,直到我find一个完全适合页面的string长度,然后在那一点上切断string? 有更聪明的方法吗? 有任何想法吗? 谢谢。

在CGContext上绘制部分透明的图像

我在前台和透明背景中有一个黄色花瓶的图像: 我正在CGContext上绘制它: CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), myImage.CGImage); 我可以在CGContextDrawImage之前使用下面的语句在它周围画一个阴影: CGContextSetShadowWithColor(context, CGSizeMake(0,0), 5, [UIColor blueColor].CGColor); 但是我想在图片上放一下,所以它看起来像下面这样: 如果我这样做: CGContextSetRGBStrokeColor(shadowContext, 0.0f, 0.0f, 1.0f, 1.0f); CGContextSetLineWidth(shadowContext, 5); CGContextStrokeRect(shadowContext, CGRectMake(0, 0, 100, 100)); 它(显然)在整个图像周围绘制一个矩形边框,如下所示: 这不是我所需要的。 但是,在第三个图像中绘制边界的最好方法是什么? 请注意,在这种情况下不可能使用UIImageView,所以使用UIImageView的CALayer属性是不适用的。

自定义MKOverlayView线宽

我有一个自定义的MKOverlayView,它绘制了一个简单的线条。 在drawMapRect:我正在设置CGContextSetLineWidth(context, 30); 完全放大时看起来很好,但当缩小时,线条变得越来越薄。 MKOverlayView引用说它会自动缩放到地图的缩放级别。 无论我的缩放级别如何,我可以强制它绘制相同的宽度? 我注意到,MKPolylineView(和MKPolygonView)正确地做到了这一点…只要你缩放调整以保持相同的宽度,你可以看到线的宽度resize。 我怎么能在我的overlayView中做到这一点?

iOS到Mac GraphicContext解释/转换

我已经在iOS上编程了两年,从来没有在Mac上编程。 我正在处理一些小工具,用于处理iOS开发中的一些简单的图像需求。 无论如何,我有在iOS的工作代码运行完美,但我绝对不知道是什么等同于Mac。 我已经尝试了一堆不同的东西,但我真的不明白如何在“drawRect:”方法之外的Mac上启动graphics上下文。 在iPhone上,我只是使用UIGraphicsBeghinImageContext()。 我知道其他职位已经说过使用lockFocus / unlockFocus,但我不知道如何做到这一点为我的需要工作。 哦,我真的很想念UIImage的“CGImage”属性。 我不明白为什么NSImage不能有一个,虽然听起来有点棘手。 这里是我在iOS上的工作代码 – 基本上它只是从掩码中创build一个reflection的图像,并将它们组合在一起: UIImage *mask = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Mask_Image.jpg" ofType:nil]]; UIImage *image = [UIImage imageNamed::@"Test_Image1.jpg"]; UIGraphicsBeginImageContextWithOptions(mask.size, NO, [[UIScreen mainScreen]scale]); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(ctx, 0.0, mask.size.height); CGContextScaleCTM(ctx, 1.f, -1.f); [image drawInRect:CGRectMake(0.f, -mask.size.height, image.size.width, image.size.height)]; UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef maskRef = mask.CGImage; CGImageRef maskCreate […]

通过在后台线程 – iPhone上渲染UIWebView来创buildUIImage

是否有人知道如何获得从UIWebView呈现的UIImage的方式或创意? 问题在于,这必须在后台线程上。 我将详细说明:我试图从多个UIWebViews每秒获取图像 – ,并将其显示在屏幕上(当然是iPhone)。 因为渲染一个图层到CGContext是一个CPU消耗的工作,我不想使用主线程,所以不要挂用户界面。 我到目前为止的尝试是: 我试图在webView的图层上使用renderInContext到UIGraphicalContext ,但是_WebTryThreadLock错误使webviews崩溃。 我试图创build一个CGBitmapContext ,并呈现webview的图层,但得到了相同的结果。 我尝试了一个copy方法(通过添加一个类别) CALayer ,深层复制所有的公共属性和子层。 之后,我尝试renderInContext我复制的图层。 我得到了一个部分 “正确”的UIImage – 意思是,并不是所有的图层都被渲染,例如,我只能得到网站的页眉(或页脚,或正文,或search栏,或只是一些框架)。 UIWebview的图层由各种types的CALayers组成,所以这可能是为什么这个接近不起作用。 我尝试在kCATransactionDisableActions中设置CATransaction ,但似乎并没有改变这种行为(两者都没有)。 我相当接近放弃。 你们中间有救世主吗?