我在调试区域出现此错误,即使我的playground文件为空:
9月23日11:03:50 MyPlayground [68315]:CGContextSaveGState:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量.
9月23日11:03:50 MyPlayground [68315]:CGContextTranslateCTM:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量.
9月23日11:03:50 MyPlayground [68315]:CGContextRestoreGState:无效的上下文0x0.如果要查看回溯,请设置CG_CONTEXT_SHOW_BACKTRACE环境变量.
我看到了修改plist文件的答案.但是,我使用的是.playground文件,而不是普通的iOS项目..playground中没有plist文件.任何解决方案?
我在用Xcode Version 7.0 (7A220).
CALayer *sublayer = [CALayer layer];
/*sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;*/
sublayer.frame = CGRectMake(30, 100, 256, 256);
sublayer.contents = (id)[[UIImage imageNamed:@"moon.png"] CGImage];
[self.view.layer addSublayer:sublayer];
//self.view.backgroundColor = [UIColor blackColor];
//add moon mask
UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), YES, 1);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextClearRect(contextRef, CGRectMake(0, 0, 400, 400));
CGContextSetRGBFillColor(contextRef, 1, 1, 1, 0.8);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 0, 0.5);
CGRect ellipse = CGRectMake(50, 50, 128, 128);
CGContextAddEllipseInRect(contextRef, ellipse);
CGContextFillEllipseInRect(contextRef, ellipse);
CALayer* sublayer2 = …Run Code Online (Sandbox Code Playgroud) 我有一个图像,前景中有一个黄色花瓶,透明背景:

我在CGContext上绘制它:
CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), myImage.CGImage);
Run Code Online (Sandbox Code Playgroud)
我可以使用以下语句在它周围画一个阴影CGContextDrawImage:
CGContextSetShadowWithColor(context, CGSizeMake(0,0), 5, [UIColor blueColor].CGColor);
Run Code Online (Sandbox Code Playgroud)

但我想在图像周围放一个笔画,这样看起来就像是:

如果我这样做:
CGContextSetRGBStrokeColor(shadowContext, 0.0f, 0.0f, 1.0f, 1.0f);
CGContextSetLineWidth(shadowContext, 5);
CGContextStrokeRect(shadowContext, CGRectMake(0, 0, 100, 100));
Run Code Online (Sandbox Code Playgroud)
它(显然)围绕整个iamge绘制一个矩形边框,如下所示:
这不是我需要的.
但是在第三张图片中绘制边框的最佳方法是什么?
请注意,在这种情况下无法使用UIImageView,因此使用UIImageView的CALayer属性不适用.
您可以使用以下代码使用Quartz绘制弧:
CGContextMoveToPoint(context2, x, y);
CGContextAddArcToPoint(context2, x1, y1, x2, y2, r);
Run Code Online (Sandbox Code Playgroud)
在这些函数中,(x,y)起点r是弧半径但是什么是(x1,y1)和(x2,y2)?
我的课程是在屏幕外渲染图像.我认为重复使用CGContext而不是一次又一次地为每个图像创建相同的上下文将是一件好事.我设置了一个成员变量,_imageContext所以我只需要创建一个新的上下文,如果_imageContext是这样的话:
if(!_imageContext)
_imageContext = [self contextOfSize:imageSize];
Run Code Online (Sandbox Code Playgroud)
代替:
CGContextRef imageContext = [self contextOfSize:imageSize];
Run Code Online (Sandbox Code Playgroud)
当然我不再发布CGContext了.
这些是我所做的唯一改变,结果是重用上下文减慢了从大约10ms到60ms的渲染速度.我错过了什么吗?在重新插入之前,我是否必须清除上下文或其他内容?或者它是重建每个图像的上下文的正确方法?
编辑
找到最奇怪的连接..
当我在应用程序开始渲染图像时,我正在寻找应用程序内存难以置信地增加的原因,我发现问题在于我将渲染图像设置为NSImageView.
imageView.image = nil;
imageView.image = [[NSImage alloc] initWithCGImage:_imageRef size:size];
Run Code Online (Sandbox Code Playgroud)
看起来ARC并没有发布以前的版本NSImage.避免这种情况的第一种方法是将新图像绘制成旧图像.
[imageView.image lockFocus];
[[[NSImage alloc] initWithCGImage:_imageRef size:size] drawInRect:NSMakeRect(0, 0, size.width, size.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[imageView.image unlockFocus];
[imageView setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)
内存问题已经消失了,并且CGContext-reuse问题发生了什么?不重复使用上下文现在需要20ms而不是10ms - 当然,绘制到图像所需的时间比设置它要长.重用上下文也需要20ms而不是60ms.但为什么?我没有看到可能有任何连接,但我可以重现旧的状态,重用只需要设置NSImageView图像而不是绘制图像.
我试图使用以下代码擦除图像
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();
Run Code Online (Sandbox Code Playgroud)
但我发现由于drawInRect,Image会失去分辨率.

我已经在网上搜索了几天,试图找到关于如何在Swift中以程序方式绘制矩形或线条的最简单的代码示例.我已经看过如何通过覆盖DrawRect命令来完成它.我相信你可以创建一个CGContext然后绘制成一个图像,但我很想看到一些简单的代码示例.或者这是一种可怕的方法?谢谢.
class MenuController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.blackColor()
var logoFrame = CGRectMake(0,0,118,40)
var imageView = UIImageView(frame: logoFrame)
imageView.image = UIImage(named:"Logo")
self.view.addSubview(imageView)
//need to draw a rectangle here
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在PDF中创建页面的预览图像但我在内存释放方面遇到了一些问题.
我写了一个循环问题的简单测试算法, 应用程序在第40次迭代附近崩溃:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"];
CFURLRef url = CFURLCreateWithFileSystemPath( NULL, (CFStringRef)pdfPath, kCFURLPOSIXPathStyle, NO );
CGPDFDocumentRef myPdf = CGPDFDocumentCreateWithURL( url );
CFRelease (url);
CGPDFPageRef page = CGPDFDocumentGetPage( myPdf, 1 );
int i=0;
while(i < 1000){
UIGraphicsBeginImageContext(CGSizeMake(768,1024));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,CGRectMake(0, 0, 768, 1024));
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, 1024);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
// --------------------------
// The problem is here (without this line …Run Code Online (Sandbox Code Playgroud) 我有一个UIView自定义形状绘制drawRect:.该frame属性设置为:
{5.f, 6.f, 50.f, 50.f}
Run Code Online (Sandbox Code Playgroud)
现在,我在图像上下文中渲染视图,但忽略了它的框架属性UIView,并始终UIView在左上角绘制.
[_shapeView.layer renderInContext:UIGraphicsGetCurrentContext()];
Run Code Online (Sandbox Code Playgroud)
我试图改变frame的CALayer,但什么都没有改变.修改bounds财产使事情变得更糟.我发现有用的唯一工作是:
CGRect frame = _shapeView.frame;
CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
[_shapeView.layer renderInContext:context];
Run Code Online (Sandbox Code Playgroud)
但是,当我处理许多形状时,这是不切实际的,我想只使用frame属性.
任何人都知道如何在不使用Core-Plot但使用内置框架的情况下在iOS上创建折线图?拼命寻找答案.