Xcode 6.1
OS X 10.9.5
Swift Mac基于文档的应用程序目标
我正在使用Swift制作一个Cocoa Mac应用程序(不是iOS!).我有一个自定义NSView子类.我想覆盖-drawRect:,并CGContext使用CoreGraphics API 访问当前进行绘图.
但是,我似乎无法访问CGContextSwift中的当前内容(我在ObjC中已经完成了数百次).这是我的代码:
import Cocoa
class Canvas: NSView {
override func drawRect(dirtyRect: CGRect) {
if let ctx: CGContext! = NSGraphicsContext.currentContext()?.CGContext {
// do drawing
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行时,我的drawRect实现的第一行立即崩溃:
-[NSWindowGraphicsContext CGContext]: unrecognized selector sent to instance 0x60800005e840
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如何获取CGContextSwift绘图的当前值?
我肯定想使用CoreGraphics API.除非在Swift中使用CoreGraphics API是完全不可能的,否则请不要回答有关如何使用AppKit绘图API的建议(我不在乎它们是否更容易).我想知道如何在OS X上使用Swift的CoreGraphics.
任何人都知道为什么CGContextFillPath不能在下面的代码片段中工作?我正在使用以下代码绘制到UIImageView.它正确地描述了路径,但忽略了CGContextFillPath.
UIGraphicsBeginImageContext(self.frame.size);
[drawingView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), firstMovedTouch.x, firstMovedTouch.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFillPath(UIGraphicsGetCurrentContext());
drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud) 我想在表格视图单元格中绘制线条,以便我可以将textfield和switch放在单个单元格中.我增加了细胞的高度.我怎样才能在细胞中划线?
我有UIView的子类,其中包含以下代码
//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the stroke (pen) color
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//Set the width of the pen mark
CGContextSetLineWidth(context, 1.0);
// Draw a line
//Start at this point
CGContextMoveToPoint(context, 10.0, 30.0);
//Give instructions to the CGContext
//(move "pen" around the screen)
CGContextAddLineToPoint(context, 310.0, 30.0);
//Draw it
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud)
然后我有一个tableViewController与分组表格样式.在cellForRowAtIndexPath中,我有以下代码
//code to add textfield
DrawLineView *drawLine = [[DrawLineView alloc]init];
[cell addSubview:drawLine];
//code add switch
Run Code Online (Sandbox Code Playgroud)
但它没有划清界线.我不能使用2个不同的细胞.我得帮助我.这是我第一次处理iphone的图形.谢谢
我需要绘制以下图像 
灰色部分是我想在另一个图像上绘制我需要使用CGContext方法使用的代码,我尝试使用CGContextAddArc但是失败了因为当我填充笔划时,中心凹陷也填充了灰色纹理.
任何帮助赞赏.
信息:我有完整的蓝色图像,我需要在蓝色图像上方添加半圆
谢谢
我一直在尝试iOS绘图.为了做一个实际练习,我写了一个BarChart组件.以下是类图(好吧,我不允许上传图像)所以让我用文字写.我有一个NGBarChartView,它继承自UIView有2个协议NGBarChartViewDataSource和NGBarChartViewDelegate.代码位于https://github.com/mraghuram/NELGPieChart/blob/master/NELGPieChart/NGBarChartView.m
为了绘制barChart,我创建了每个barChart作为不同的CAShapeLayer.我这样做的原因有两个,首先我可以创建一个UIBezierPath并将其附加到CAShapeLayer对象和两个,我可以通过使用[Layer hitTest]方法轻松跟踪是否触摸了barItem.该组件运行良好.但是,我对绘制barCharts的方法感到不舒服.因此这个说明.我需要以下专家意见
非常感谢帮助.顺便说一句,你可以自由地查看我的代码和评论.我很乐意接受任何改进的建议.
最好的,Murali
我已经在iOS上编程了两年而从未在mac上编程.我正在开发一个小工具,用于处理我在iOS开发中的一些简单的图像需求.无论如何,我在iOS中运行的代码运行完美,但我完全不知道mac的等价物.
我尝试了很多不同的东西,但我真的不明白如何在"drawRect:"方法之外的Mac上启动图形上下文.在iPhone上我只会使用UIGraphicsBeghinImageContext().我知道其他帖子曾说过使用lockFocus/unlockFocus,但我不确定如何根据我的需要做到这一点.哦,我真的很想念UIImage的"CGImage"属性.我不明白为什么NSImage不能有一个,虽然听起来有点棘手.
这是我在iOS上的工作代码 - 基本上它只是从掩码中创建一个反射图像并将它们组合在一起:
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 = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([flippedImage CGImage], maskCreate);
CGImageRelease(maskCreate);
UIImage *maskedImage = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, image.size.height + (image.size.height * .5)), NO, [[UIScreen mainScreen]scale]);
[image drawInRect:CGRectMake(0,0, …Run Code Online (Sandbox Code Playgroud) 如何通过在手指上滑动来擦除前面UIImage上的内容并显示UIView背面的UIImage.
我曾使用以下代码- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 擦除前图像:
-(void)eraseDrawingContents
{
UIGraphicsBeginImageContext(frontImage.frame.size);
[frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)];
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), 50, [[UIColor whiteColor]CGColor]);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, lastPoint.x, lastPoint.y);
CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextAddPath(UIGraphicsGetCurrentContext(), path);
CGContextStrokePath(UIGraphicsGetCurrentContext());
frontImage.image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
Run Code Online (Sandbox Code Playgroud)
我实现了以下输出.
但我需要像这样的输出.

我们如何实现这一功能?
请帮我.
我想创造不同的形状.下图是一个例子.你能告诉我如何塑造不同的形状吗?

我希望绘制一个UILabel(最好通过子类)作为透明标签,但具有坚实的背景.我画了一个简单的例子(对不起,这很难看,但它得到的分数:)).
基本上我有一个UILabel,我希望背景是一个设置颜色,文本应该是透明的.我不想用视图背景为文本着色,而是让它是100%透明的,因为我在背景中有一个纹理,我想确保标签内外的线条.
我一直在深夜浏览SO并在Google上搜索,但我找不到有用的消息来源.我对CG绘图没有太多经验,所以我很感激任何链接,帮助,教程或示例代码(也许Apple有一些我需要看一下?).
谢谢你!

我正在尝试复制Instagram的功能,你有你的照片,你可以添加贴纸(其他图像),然后保存.
所以在UIImageView我的照片上,我将贴纸(另一个UIImageView)添加到它作为子视图,位于父级的中心UIImageView.
要在图片周围移动贴纸,我使用a CGAffineTransform(我不移动中心UIImageView).我还申请CGAffineTransform旋转和缩放贴纸.
要使用贴纸保存图片,我使用CGContext如下:
extension UIImage {
func merge2(in rect: CGRect, with imageTuples: [(image: UIImage, viewSize: CGSize, transform: CGAffineTransform)]) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
draw(in: CGRect(size: size), blendMode: .normal, alpha: 1)
// Those multiplicators are used to properly scale the transform of each sub image as the parent image (self) might be bigger than its …Run Code Online (Sandbox Code Playgroud) cgcontext ×10
ios ×5
iphone ×4
drawrect ×2
macos ×2
swift ×2
calayer ×1
cashapelayer ×1
cocoa ×1
converter ×1
geometry ×1
objective-c ×1
quartz-2d ×1
uibezierpath ×1
uiimageview ×1
uilabel ×1
xcode ×1