如何使用 Quartz 在现有图像之上绘制来创建新图像?

发布于 2024-08-05 20:56:04 字数 526 浏览 3 评论 0原文

我有一个 uiimageview 视图,我通过相机分配这个 uiimageview 图像..现在我想在图像上做一些绘图....使用 coregraphics.我想做这样的事情...通过触摸和画线选择一个区域线连接诸如圆形或任何形状之类的东西..我想将该特定区域更改为其他东西,例如更改那里的颜色。将其变成灰度..到目前为止我能够画线...这是线的图像在 uiimage 视图上绘制...

替代文字

但我无法弄清楚如何在 imageview 的图像上绘制..意味着如何修改 imageview 的图像???

我也想在单击“清除”按钮或“撤消”之类的按钮时恢复图像。有人知道如何实现此目的吗?

单击裁剪按钮时如何

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

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

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?

and

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..

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

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

发布评论

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

评论(3

冷夜 2024-08-12 20:56:04

步骤如下:

  1. 创建与图像的颜色空间和尺寸匹配的 CGBitmapContext。
  2. 将图像绘制到该上下文中。
  3. 在图像上绘制任何您想要的内容。
  4. 从上下文创建一个新图像。
  5. 处理掉上下文。

下面是一个方法,它获取一个图像,在其上绘制一些内容,然后返回一个包含修改内容的新 UIImage:

- (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;
}

要恢复到旧图像,只需保留对其的引用即可。

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

These are the steps:

  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.

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.

青萝楚歌 2024-08-12 20:56:04

更简单的解决方案是

(UIImage *) modifyImage:(UIImage *)inputImage
{
   UIGraphicsBeginImageContext(inputImage.size);
   [inputImage drawInRect:CGRectMake(0, 0, inputImage.size.width, inputImage.size.height);
   CGContextRef ctx = UIGraphicsGetCurrentContext();
   //Drawing code using above context goes here
   /*
    *
    */
   UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return outputImage;
}

A lot easier solution would be

(UIImage *) modifyImage:(UIImage *)inputImage
{
   UIGraphicsBeginImageContext(inputImage.size);
   [inputImage drawInRect:CGRectMake(0, 0, inputImage.size.width, inputImage.size.height);
   CGContextRef ctx = UIGraphicsGetCurrentContext();
   //Drawing code using above context goes here
   /*
    *
    */
   UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return outputImage;
}
梦在夏天 2024-08-12 20:56:04

看看Quartz 2D 概述,了解有关在 iPhone 上使用 Quartz 2D 的信息。

Take a look at Overview of Quartz 2D for information on using Quartz 2D on iPhone.

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