【问题标题】:Drawing a DOT within a cells UIImageView's UIImage在单元格 UIImageView 的 UIImage 中绘制 DOT
【发布时间】:2023-03-09 02:08:01
【问题描述】:

目前,我正在尝试在单元格的UIImageViewUIImage 内绘制一个简单的点。当事件由日历表示时,我正在尝试完成与 iPhone 的日历应用程序相同的事情。

[[event calendar] CGColor] 

是一个记录UIDeviceRGBColorSpace 0.509804 0.584314 0.686275 1EKEvent对象

我尝试创建一个颜色为[[event calendar] CGColor] 的小点。这是我目前正在尝试做的事情:

CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(contextRef, [[event calendar] CGColor]);
CGContextAddEllipseInRect(contextRef,(CGRectMake (12.f, 5.f, 4.f, 5.f)));
CGContextDrawPath(contextRef, kCGPathFill);
CGContextStrokePath(contextRef);
cell.imageViewPic.image = UIGraphicsGetImageFromCurrentImageContext();

但这是我得到的错误:

 MYSCHEDULER[21445] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
 MYSCHEDULER[21445] <Error>: CGContextAddEllipseInRect: invalid context 0x0
 MYSCHEDULER[21445] <Error>: CGContextDrawPath: invalid context 0x0
 MYSCHEDULER[21445] <Error>: CGContextDrawPath: invalid context 0x0

我不明白我做错了什么...为什么上下文无效?

【问题讨论】:

    标签: iphone ios objective-c uiimage cgcontext


    【解决方案1】:

    正如我所见,您正在尝试获取您所做的绘图的图像,然后将其添加到 imageViewPic。以下是您应该做的事情:

    UIGraphicsBeginImageContext(CGSizeMake(5.0f,5.0f)); 
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contextRef, [[event calendar] CGColor]);
    CGContextFillEllipseInRect(contextRef,(CGRectMake (0.f, 0.f, 5.f, 5.f)));
    UIImage *drawingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    cell.imageViewPic.image = drawingImage;
    

    【讨论】:

    • UIGraphicsBeginImageContext();给我一个错误。 “函数调用的参数太少,未指定单个参数‘大小’”
    • 我忘了在第一行添加尺寸。现在它将起作用。请检查我的编辑。
    • 是的,我只是查看了它并添加了单元格 imageViewPic 的大小...谢谢@CODENAMELAMBDA1 你太棒了!
    • 感谢您的赞赏! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多