我找到了一个Objective-C代码示例,它可以在这里得到一个像素的颜色: 如何在触摸时获得像素颜色?
我需要帮助的特定代码部分是使用CGColorSpaceCreateDeviceRGB创建上下文的地方:
---这是Objective-C代码
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
Run Code Online (Sandbox Code Playgroud)
我最好的尝试看起来如下(我没有返回任何东西,但我正试图先正确地获取上下文):
---这是我对Swift转换的最佳尝试
func getPixelColorAtPoint()
{
let pixel = UnsafeMutablePointer<CUnsignedChar>.alloc(1)
var colorSpace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
let context = CGBitmapContextCreate(pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: nil, bitmapInfo: CGImageAlphaInfo.PremultipliedLast)
}
Run Code Online (Sandbox Code Playgroud)
但是这给了我一个错误
Cannot convert the expression's type '(UnsafeMutablePointer<CUnsignedChar>, width: IntegerLiteralConvertible, height: IntegerLiteralConvertible, bitsPerComponent: IntegerLiteralConvertible, bytesPerRow: IntegerLiteralConvertible, space: NilLiteralConvertible, bitmapInfo: CGImageAlphaInfo)' to type 'IntegerLiteralConvertible' …Run Code Online (Sandbox Code Playgroud) 今天早上我正在移植一些ObjectiveC自定义UIView子类Swift。为了使其更加“面向对象”,我遵循了使用CGContext方法扩展的示例。例如
extension CGContext {
func fillColor(color:UIColor) {
CGContextSetFillColorWithColor(self, color.CGColor)
}
}
Run Code Online (Sandbox Code Playgroud)
因为我正在将目标 C 风格的消息(例如-(void) drawOutline: (CGContextRef) cr {...})转换为 Swift 风格的消息,而没有太注意(例如func drawOutline(cr:CGContextRef) {...}),所以我一开始并没有意识到我正在传递CGContextRef参数,而是进行了扩展CGContext(而不是 Ref)。但它成功了!
更奇怪的是,当我将这些CGContextRef' 更改为CGContext' 时。它仍然有效。不仅编译,而且运行和绘制正确。为了好玩,我改成了extension CGContextRef. 但它仍然继续发挥作用。就好像 Swift 足够聪明,可以将它们归结为同一件事。是吗?或者这里发生了更微妙/更酷的事情?
因为如果运行Instruments并选择了活动监视器,则在iPhone 4S上运行的应用程序在发布上下文时使用4.88MB,如果未发布上下文,则使用4.88MB .这是否意味着释放上下文是可选的?(我认为实际上是必需的).上下文由CGContextRef变量引用.(正在使用ARC).
CGBitmapContext为Retina显示器创建了上下文,因此大约640 x 640,并且有4个这样的上下文,它们都是在中创建的viewDidAppear,我认为如果1个像素是4个字节,那么每个上下文已经是1.6MB.可能是在viewDidAppear完成后,上下文会自动释放?基本上,我CGImage从那些位图上下文生成对象并将CGImage对象设置为由对象指向CALayer(使用layer.contents = (__bridge id) cgImage;),因此不再需要位图上下文.它使用Xcode 4.3编译,使用ARC,并针对iOS 4.3.(但我认为CGContextRef不是ARC的一部分).
更新:更正:应该是"从那些CGBitmapContext生成CGImage对象,并将这些CGImage对象设置为CALayer"(编辑原始问题以反映这一点).
core-animation core-graphics ios automatic-ref-counting cgcontextref
我尝试在 iPad 上创建一个简单的手指绘画应用程序。我能够正确绘制到屏幕的路径,但我想要一个选项可以完全清除屏幕上所有绘制的路径。我当前拥有的代码清除了上下文,但是当我调用绘图代码块时,所有路径都会重新出现在屏幕上。
- (void)drawRect:(CGRect)rect
{
//Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
if(clearContext == 0){
CGContextSetLineWidth(context, 6);
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextAddPath(context, drawingPath);
CGContextStrokePath(context);
}
if(clearContext == 1){
//This is the code I currently have to clear the context, it is clearly wrong
//Just used as experimentation.
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextAddPath(context, drawingPath);
CGContextStrokePath(context);
clearContext = 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试绘制一个矩形,它应该有宽度为5.0的黑色边框,我得到如下所示的矩形,
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextStrokePath(context);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5);
CGContextFillRect(context, rect);
Run Code Online (Sandbox Code Playgroud)

我可以使它清晰/透明(白色)背景而不是现在显示的绿色背景,[UIColor whiteColor].CGColor但它也应该有黑色边框.
如何将自定义边框设置为矩形?