标签: cgcontext

改变UIImage颜色

我试图改变UIImage的颜色.我的代码:

-(UIImage *)coloredImage:(UIImage *)firstImage withColor:(UIColor *)color {
    UIGraphicsBeginImageContext(firstImage.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [color setFill];

    CGContextTranslateCTM(context, 0, firstImage.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGRect rect = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height);
    CGContextDrawImage(context, rect, firstImage.CGImage);

    CGContextClipToMask(context, rect, firstImage.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathElementMoveToPoint);

    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return coloredImg;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但获得的图像不是很好:返回图像的边界像素是间歇性的,不像我的第一张图像那么平滑.我该如何解决这个问题?

iphone objective-c uiimage cgcontext ios

87
推荐指数
6
解决办法
8万
查看次数

UISearchBar CGContext错误

我在视图中有一个UISearchBar,每当我点击它,键盘出现后 -

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

它将此发送到控制台:

<错误> CGContextSetStrokeColorWithColor::无效的上下文0x0.这是一个严重的错误.该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降低.此通知是礼貌的:请解决此问题.它将成为即将到来的更新中的致命错误.

它重复相同的错误.我想知道究竟是什么问题?

我相信有一个NULL上下文,但它与UISearchBar有什么关系?TNX.

cocoa-touch cgcontext uisearchbar ios

55
推荐指数
2
解决办法
2万
查看次数

Contex Drawing + Pagination

我试图将内容绘制scrollview到一个PDF上下文中,我正面临着问题pagination.

以下代码我使用过:

- (void)renderTheView:(UIView *)view inPDFContext:(CGContextRef)pdfContext
{
    // Creating frame.
    CGFloat heightOfPdf = [[[self attributes] objectForKey:SRCPdfHeight] floatValue];
    CGFloat widthOfPdf  = [[[self attributes] objectForKey:SRCPdfWidth] floatValue];
    CGRect pdfFrame = CGRectMake(0, 0, widthOfPdf, heightOfPdf);   
    CGRect viewFrame = [view frame];

    if ([view isKindOfClass:[UIScrollView class]])
    {
        viewFrame.size.height = ((UIScrollView *)view).contentSize.height;
        [view setFrame:viewFrame];
    }
    // Calculates number of pages.
    NSUInteger totalNumberOfPages = ceil(viewFrame.size.height/heightOfPdf);

    // Start rendering.
    for (NSUInteger pageNumber = 0; pageNumber<totalNumberOfPages; pageNumber++)
    {
       // Starts our first page.
       CGContextBeginPage …
Run Code Online (Sandbox Code Playgroud)

pdf iphone cgcontext quartz-2d ios

54
推荐指数
1
解决办法
1372
查看次数

设置CGContext透明背景

我仍然在努力用CGContext绘制一条线.我实际上去绘制线条,但现在我需要将Rect的背景透明,以便现有的背景显示出来.这是我的测试代码:

(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetAlpha(context,0.0);
    CGContextFillRect(context, rect);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

iphone graphics background transparent cgcontext

50
推荐指数
3
解决办法
4万
查看次数

在UIView中剪切透明孔

展望创建一个具有它里面的透明框架,使视图背后的意见,可以通过这个透明的框架中可以看出,但是这以外的地区将不显示通过视图.所以在视图中基本上是一个窗口.

希望能够做到这样的事情:

 CGRect hole = CGRectMake(100, 100, 250, 250);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillRect(context, rect);

CGContextAddRect(context, hole);
CGContextClip(context);

CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
Run Code Online (Sandbox Code Playgroud)

但是清除不会覆盖黑色,所以整个背景都是黑色的.沿着这些方向的任何想法?

objective-c uiview cgcontext ios

45
推荐指数
8
解决办法
3万
查看次数

画一个简单的圆圈uiimage

我试着用一个简单的蓝色圆圈制作一个20x20的UIImage.我尝试使用此功能,但结果是黑色方块中的蓝色圆圈.如何移除圆圈周围的黑色方块?

功能:

+ (UIImage *)blueCircle {
    static UIImage *blueCircle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 20.f), YES, 0.0f);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);

        CGRect rect = CGRectMake(0, 0, 20, 20);
        CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor);
        CGContextFillEllipseInRect(ctx, rect);

        CGContextRestoreGState(ctx);
        blueCircle = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

    });
    return blueCircle;
}
Run Code Online (Sandbox Code Playgroud)

实际结果: 在此输入图像描述

geometry core-graphics uiimage cgcontext ios

38
推荐指数
3
解决办法
2万
查看次数

iOS绘制填充圆圈

这里不是图形程序员,所以我试图绊倒这个.我正在尝试绘制9个实心圆圈,每个圆圈都有不同的颜色,每个圆圈都有一个白色边框.UIView的框架是CGRectMake(0,0,60,60).见附图.

问题是我在每边的边界上都有"扁平斑点".以下是我的代码(来自UIView子类):

- (void)drawRect:(CGRect)rect
{
    CGRect borderRect = CGRectMake(0.0, 0.0, 60.0, 60.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(context, colorRed, colorGreen, colorBlue, 1.0);
    CGContextSetLineWidth(context, 2.0);
    CGContextFillEllipseInRect (context, borderRect);
    CGContextStrokeEllipseInRect(context, borderRect);
    CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)

如果我在drawRect中更改为CGRectMake(0,0,56,56),我只会在顶部和左侧看到平点,并且底部和右侧看起来很好.

任何人都可以建议我如何解决这个问题?在我看来,UIView正在修剪边界,但对此并不了解,我真的不知道如何修复它.

在此之前,感谢任何图形专家的建议.

在此输入图像描述

cgcontext drawrect ios

34
推荐指数
4
解决办法
4万
查看次数

用swift导致CGBitmapContextCreate错误

我试图在swift中创建一个CGContext.它编译但在运行时抛出错误.

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask)
CGColorSpaceRelease(colorSpace);

....
Run Code Online (Sandbox Code Playgroud)

错误是:

Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 96 bytes/row.
fatal error: Can't unwrap Optional.None
Run Code Online (Sandbox Code Playgroud)

cgcontext swift

29
推荐指数
4
解决办法
2万
查看次数

CGContextSaveGState与UIGraphicsPushContext

有两种drawRect方法:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    // do drawing here
    CGContextRestoreGState(context);
}
Run Code Online (Sandbox Code Playgroud)

- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    // do drawing here
    UIGraphicsPopContext(); 
}
Run Code Online (Sandbox Code Playgroud)

UIGraphicsPushContext/UIGraphicsPopContext来自UIKit, 而CGContextSaveGState/CGContextRestoreGState来自CoreGraphics.

问题:这些方法有什么区别?哪一个更好用?是否有一些证明一种方法比其他方法更好的例子,反之亦然?

objective-c cgcontext drawrect ios

24
推荐指数
1
解决办法
6453
查看次数

CGBitMapContextCreate方法导致编译器警告Xcode 5而不是Xcode 4

我刚刚将Xcode从版本4.6.2更新到5.0,在我的项目中执行一个方法(在Xcode 4.6.2中创建)后突然发出编译器警告.我已经尝试在新旧版本的Xcode中重新打开该项目,并且我已经确认相同的方法在4.6.2中没有给出警告.

以下是在Xcode 5.0中引发警告的代码行:

CGContextRef context = CGBitmapContextCreate(NULL, frame.size.width * scaleFactor, frame.size.height * scaleFactor, 8, frame.size.width * scaleFactor * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
Run Code Online (Sandbox Code Playgroud)

警告说:

"Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitMapInfo' (aka 'enum CGBitMapInfo')"
Run Code Online (Sandbox Code Playgroud)

它似乎不是弃用警告,但我对这些类不太熟悉,无法解释其含义或知道如何解决它.任何帮助表示赞赏.

xcode cgcontext cgbitmapcontextcreate ios xcode5

24
推荐指数
2
解决办法
8178
查看次数