Tag: cgcontext

iOS ARC标记为“泄漏”的以下graphics代码

(UIImage *) cropToSquare:(UIImage *)_image { if(_image.size.height < _image.size.width) { CGRect drawRect = CGRectMake(0, 0, _image.size.height, _image.size.height); UIGraphicsBeginImageContext(drawRect.size); CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGRect cropArea = CGRectMake (((_image.size.width – _image.size.height)/2), 0, _image.size.height, _image.size.height); CGContextTranslateCTM(currentContext, 0.0, _image.size.height); CGContextScaleCTM(currentContext, 1.0, -1.0); CGContextDrawImage(currentContext, drawRect, CGImageCreateWithImageInRect (_image.CGImage, cropArea)); UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return cropped; } else { return _image; } […]

为CGLayer创buildCGContext

我想在CGLayer预先渲染一些graphics,以便在将来进行快速绘制。 我发现CGLayerCreateWithContext需要一个CGContext参数。 它可以很容易地在drawRect:方法中find。 但是我需要在drawRect:之外创build一个CGLayer 我应该在哪里得到CGContext ? 我应该简单地创build临时CGBitmapContext并使用它? 更新:我需要在drawRect之外创buildCGLayer:因为我想在呈现之前初始化CGLayer。 第一次drawRect调用可以初始化一次,但对我来说这不是一个漂亮的解决scheme。

用CGContext绘图

我试图通过touchesMove:方法画线。 以下是我的动作touchesMoved: 。 UIGraphicsBeginImageContext(self.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); // context setting CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); CGContextSetLineWidth(context, 2.0); CGContextSetRGBStrokeColor(context, 255, 0, 0, 0.5); CGContextSetBlendMode(context, kCGBlendModeNormal); // drawing CGContextMoveToPoint(context, lastPoint.x, lastPoint.y); CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y); CGContextStrokePath(context); CGContextFlush(context); self.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); touchesMoved:被调用; 但是屏幕上没有显示任何内容。 我错过了什么? 添加 self是UIImageView的子类。

iOS – 调整UIImage的色调

可以调整UIImage / CGContextRef的Hue吗? 我试图将上下文与Color和Hue混合模式混合,但结果是错误的。 我应该将RGB转换成HSB,调整HSB并转换回来?

如何在CGContext CGMutablePathRef上绘制顶点?

当用户input点击来指定顶点时,我绘制了一组连接线。 我构build了一个CGMutablePathRef – (void)addPointToCGMPR: (CGPoint)p forNewPolygon: (BOOL)newPoly { if (newPoly) { CGPathMoveToPoint(cgmpr, NULL, px, py); } else { CGPathAddLineToPoint(cgmpr, NULL, px, py); } [self setNeedsDisplay]; } 然后在input每个点之后调用drawRect:并将上下文CGMutablePathRef添加到CGContext中以供显示 – (void)drawRect:(CGRect)rect { // Set up a context to display CGContextRef context = UIGraphicsGetCurrentContext(); CGContextAddPath(context, cgmpr); CGContextSetLineWidth(context, 1); CGContextStrokePath(context); // If NOT empty then Close the context so […]

CGContext转换,提取旋转的矩形

我的意图是写一个函数,可以提取一个更大的图像旋转的矩形部分。 该旋转矩形部分用中心点(从0,0到全图像宽度/高度)的大小和旋转angular度来描述。 我担心我不了解CGContexts。 我知道,旋转后,我的坐标空间旋转。 如果angular度为0,则此function始终有效,如果angular度较小,此function显示正确。 但是,随着angular度的增加,一切都被错误地抵消了。 我正在计算类似于图像变换的仿射变换来计算左上angular,以避免任何不准确。 我也尝试了通过应用图像变换来进一步改变左上angular,以及图像变换的逆(就像一个随机的想法)。 这种方法会起作用吗? 我是否错过了某处的空间转换? 注意:通过绘制完整的图像,然后在正确的位置以正确的大小和旋转画出一个黑色的矩形,非常容易使图像区域变黑。 鉴于此,我知道我的投入是正确的。 可视化: 这代表完整的形象,我正在尝试示例旋转矩形: 该图像表示此function的所需输出: – (UIImage*) croppedImage:(UIImage*) image center:(CGPoint) rotationCenter size:(CGSize) size rotation:(CGFloat) rotation { CGFloat fullWidth = image.size.width; CGFloat fullHeight = image.size.height; CGRect imageRect = CGRectMake(0, 0, fullWidth, fullHeight); //must convert UI space to CG space, in this case that just means adjusting […]

UIScrollView中的UIView的素描

当我尝试在UIScrollView的子视图UIView上绘制草图时遇到问题。 在UIView放大或滚动之后尝试在UIView上绘制草图时,会出现问题。 看起来我的草图代码(如下所示)没有考虑UIScrollView内的UIView的缩放或缩放,因为线条模糊,并没有显示他们应该在哪里。 请让我知道是否有什么可以做我的代码或提供另一种解决scheme。 – (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Draw touchesBegan"); mouseSwipedPSVC = NO; UITouch *touch = [touches anyObject]; lastPointPSVC = [touch locationInView:sheetDrawView]; } – (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwipedPSVC = YES; UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:sheetDrawView]; UIGraphicsBeginImageContext(sheetDrawView.frame.size); [drawImageView.image drawInRect:CGRectMake(0, 0, sheetDrawView.frame.size.width, sheetDrawView.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPointPSVC.x, lastPointPSVC.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, […]

将所有UILabel(具有适当的起源)组合成一个UIImage

所以我正在使用的应用程序可以让用户编写文本,应用程序将该文本转换为单独的UILabel(一个UILabel仅包含文本,另一个UILabel与文本宽度相同,但带有透明背景色) 。 我想在背景中将所有的uilabels和UIImage结合起来,创build一个新的UIImage。 到目前为止,我有下面的代码,但它不会产生任何实际的结果,并会爱上任何人对这个问题的帮助: UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 416), NO, 0.0); CGContextRef ctx = UIGraphicsGetCurrentContext(); [_imgViewFinal.layer renderInContext:ctx]; CGContextSaveGState(ctx); for (int i = 0; i < _allLabels.count; i++) { UILabel *tempLabelBg = [_allBgs objectAtIndex:i]; CGContextTranslateCTM(ctx, tempLabelBg.frame.origin.x, tempLabelBg.frame.origin.y); CGContextSaveGState(ctx); [tempLabelBg.layer renderInContext:ctx]; CGContextRestoreGState(ctx); UILabel *tempLabelText = [_allLabels objectAtIndex:i]; [tempLabelText drawTextInRect:tempLabelText.frame]; } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); NSData *imgData = UIImageJPEGRepresentation(image, 1.0); UIImage […]

为什么我的Coregraphics绘图代码造成滞后?

我正在为iPhone绘制应用程序。 它在iPhone模拟器的5秒内工作正常,但随着越来越多,我得出它更加滞后。 当我在设备上testing时,它会变得更加迟钝,我甚至无法画出一棵简单的树。 当我检查处理器在xcode上的运行百分比时,通常在97-100%之间。 有没有什么办法解决这一问题? (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; currentPoint = [touch locationInView:self.view]; UIGraphicsBeginImageContext(CGSizeMake(320, 568)); [drawImage.image drawInRect:CGRectMake(0, 0, 320, 568)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGPathMoveToPoint(path, NULL, lastPoint.x, lastPoint.y); CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y); CGContextAddPath(UIGraphicsGetCurrentContext(),path); CGContextStrokePath(UIGraphicsGetCurrentContext()); [drawImage setFrame:CGRectMake(0, 0, 320, 568)]; drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); […]

CAGradient图层在视图中不可见

我尝试在我用Core Graphics制作的饼图中添加Cagradient效果。 事实certificate,我不能够,当然不能很好地理解如何将它应用到CGPath的一部分的视图… override func draw(_ rect: CGRect) { … func arc(myRadius: CGFloat, myStartAngle: CGFloat, myEndAngle: CGFloat) { // This is the pie chart segment context?.setFillColor(phenotypeColor.cgColor) context?.move(to: center) context?.addArc(center: center, radius: finalRadius, startAngle: myStartAngle, endAngle: myEndAngle, clockwise: true) context?.fillPath() (…) let gradientLayer = CAGradientLayer() gradientLayer.frame = self.bounds gradientLayer.colors = [UIColor.red, UIColor.blue] self.layer.addSublayer(gradientLayer) } 我没有看到屏幕上的任何渐变(只有白色的背景…)。 最后,我还试图在保存状态后clip上下文,但我不确定是否剪辑了任何东西,因为我没有看到任何渐变(无论是在整个视图还是在片段上)。 […]