如何使用Quartz在现有图像上绘图来创建新图像? [英] How do I create a new image by drawing on top of an existing one using Quartz?

查看:148
本文介绍了如何使用Quartz在现有图像上绘图来创建新图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图与uiimageview我通过相机分配这个uiimageview图像..现在我想做一些绘图到图像....使用coregraphics.i希望做这样的事情...通过触摸和选择一个区域当线加入像圆或任何形状的东西时绘制线。我想将那个特定区域更改为其他东西,例如更改颜色。将其转换为灰度..直到现在我能画线...这里是一个在uiimage视图上绘制的线条图像...

i have a view with uiimageview i assign this uiimageview image by camera..now i want to do some drawing onto image....using coregraphics.i want to do something like this... select an area by touching and drawing line when line joins something like circle or any shape..i want to change that particular area in to something else for example change color there.turn that into grayscale.. till now i am able to draw line...here is an image of line drawn over a uiimage view...

alt text http: //i29.tinypic.com/x5quc6.png

但是我无法弄明白我如何在imageview的图像中绘制..如何修改imageview的图像???

but i am unable to figure it out how do i draw at imageview's image..mean how to modify imageview's image???

我想在点击清除按钮或类似撤消之类的东西时恢复图像。有人知道如何实现这个目标吗?

also i want to restore image when click on clear button or something like undo..does someone knows how to achieve this?

如何在点击裁剪按钮时创建一个矩形,将矩形移动到屏幕上的任何位置......日按下按钮裁剪图像...然后保存裁剪的图像..

how do i create a rectangle when click on crop button move the rectangle any where on the screen...and then push the button to crop the image...and then save cropped image..

推荐答案

这些是以下步骤:


  1. 创建一个与图像颜色空间和尺寸相匹配的CGBitmapContext。

  2. 将图像绘制到该上下文中。

  3. 在图像上绘制您想要的任何内容。

  4. 从上下文创建新图像。

  5. Dispose关闭上下文。

  1. Create a CGBitmapContext matching the image's colorspace and dimensions.
  2. Draw the image into that context.
  3. Draw whatever you want on top of the image.
  4. Create a new image from the context.
  5. Dispose off the context.

这是一个获取图像,在其上绘制并返回带有修改内容的新UIImage的方法:

Here's a method that takes an image, draws something on top of it and returns a new UIImage with modified contents:

- (UIImage*)modifiedImageWithImage:(UIImage*)uiImage
{
    // build context to draw in
    CGImageRef image = uiImage.CGImage;
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL,
                                             CGImageGetWidth(image), CGImageGetHeight(image),
                                             8, CGImageGetWidth(image) * 4,
                                             colorspace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colorspace);

    // draw original image
    CGRect r = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
    CGContextSetBlendMode(ctx, kCGBlendModeCopy);
    CGContextDrawImage(ctx, r, image);
    CGContextSetBlendMode(ctx, kCGBlendModeNormal);

    // draw something
    CGContextAddEllipseInRect(ctx, CGRectInset(r, 10, 10));
    CGContextSetRGBStrokeColor(ctx, 1.0f, 1.0f, 1.0f, 0.5f);
    CGContextSetLineWidth(ctx, 16.0f);
    CGContextDrawPath(ctx, kCGPathStroke);

    CGContextAddEllipseInRect(ctx, CGRectInset(r, 10, 10));
    CGContextSetRGBStrokeColor(ctx, 0.7f, 0.0f, 0.0f, 1.0f);
    CGContextSetLineWidth(ctx, 4.0f);
    CGContextDrawPath(ctx, kCGPathStroke);

    // create resulting image
    image = CGBitmapContextCreateImage(ctx);
    UIImage* newImage = [[[UIImage alloc] initWithCGImage:image] autorelease];
    CGImageRelease(image);
    CGContextRelease(ctx);

    return newImage;
}

要恢复旧图像,只需保留对它的引用。

To restore to old image, just keep a reference to it.

裁剪的事情与上述内容无关,你应该为此创建一个新问题。

The cropping thing is not related to the above and you should create a new question for that.

这篇关于如何使用Quartz在现有图像上绘图来创建新图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆