iPhone sdk 保存绘图时没有完整的上下文

发布于 2024-12-07 20:53:54 字数 1351 浏览 1 评论 0原文

我想从我的 iPhone 应用程序捕获我的视图屏幕。我有白色背景视图,并使用此方法在该视图的图层上画一条线。

- (void)draw {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    if (self.fillColor) {
        [self.fillColor setFill];
        [self.path fill];
    }
    if (self.strokeColor) {
        [self.strokeColor setStroke];
        [self.path stroke];
    }

    CGContextRestoreGState(context);
}



- (void)drawRect:(CGRect)rect {
    // Drawing code.
    for (<Drawable> d in drawables) {
        [d draw];
    }
    [delegate drawTemporary];
}

我使用委托方法在图层上画线。 这是我从中获得帮助的项目链接。 https://github.com/search ?q=dudel&type=Everything&repo=&langOverride=&start_value=1

现在,当我使用以下上下文方法仅保存绘图时,我成功保存了它,而没有白色 背景。

drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

但是当我使用 Bezier Pathe 的以下方法时,我无法保存没有白色背景的绘图,它保存了整个屏幕,即该绘图及其背景。

UIGraphicsBeginImageContext(self.view.bounds.size);
    [dudelView.layer renderInContext:UIGraphicsGetCurrentContext()];
    //UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();

那么任何人都可以帮助我如何仅在此应用程序中保存绘图。

I want to capture screen of my view from my iPhone app. I have white background view and on that I draw a lines on that view's layer using this method .

- (void)draw {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    if (self.fillColor) {
        [self.fillColor setFill];
        [self.path fill];
    }
    if (self.strokeColor) {
        [self.strokeColor setStroke];
        [self.path stroke];
    }

    CGContextRestoreGState(context);
}



- (void)drawRect:(CGRect)rect {
    // Drawing code.
    for (<Drawable> d in drawables) {
        [d draw];
    }
    [delegate drawTemporary];
}

I have use delegate methods to draw lines on layer.
This is the project link from where I get help for this.
https://github.com/search?q=dudel&type=Everything&repo=&langOverride=&start_value=1

Now when I use the following context methods to save the drawing only I successfully save it without that white background.

drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

But When I use the following method of Bezier Pathe I cannot save the drawing without its white background,It saves the whole screen i.e. that drawing and its background.

UIGraphicsBeginImageContext(self.view.bounds.size);
    [dudelView.layer renderInContext:UIGraphicsGetCurrentContext()];
    //UIImage *finishedPic = UIGraphicsGetImageFromCurrentImageContext();

So can anybody help me how can I save the drawing only here in this app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-12-14 20:53:54

(您已将其标记为与 MapKit 相关,但没有提及 MapKit。)

为什么不将绘图序列分成三个块呢?

  1. 将路径绘制到图像上下文中并获取 UIImage,如您所描述的。
  2. 绘制背景颜色。
  3. 绘制 UIImage。

然后你也可以使用 UIImage 作为你的“屏幕截图”。

我还应该注意,如果您在捕获的 UIImage 中唯一不想要的东西是背景颜色,那么您最好创建一个 UIImageView,设置其背景颜色 (-setBackgroundColor:< /code>),并将其图像设置为您的 UIImage。

UIImageView 内部有许多优化,使其能够以比自定义 -drawRect: 实现更高的性能显示图形。

(You've tagged this as MapKit related, but make no mention of MapKit.)

Why don't you just split your drawing sequence into three chunks?

  1. Draw your paths into an image context and get a UIImage, as you described.
  2. Draw a background color.
  3. Draw the UIImage.

Then you can use the UIImage for your "screenshot" as well.

I should also note that if the only thing you don't want in your captured UIImage is the background color, you are better off creating a UIImageView, setting its background color (-setBackgroundColor:), and setting its image to be your UIImage.

UIImageView internally has a number of optimizations that allow it to display graphics with much higher performance than you can get with a custom -drawRect: implementation.

再见回来 2024-12-14 20:53:54

为什么不直接保存可绘制数组呢?这些都是没有底层图像的图画:)

Why don't you just save the drawables array? Those are all the drawings without the underlying image :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文