How do I create a new image by drawing on top of an existing one using Quartz?

前端 未结 3 1124
一向
一向 2021-02-10 11:15

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

相关标签:
3条回答
  • 2021-02-10 11:52

    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.

    0 讨论(0)
  • 2021-02-10 12:03

    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;
    }
    
    0 讨论(0)
  • 2021-02-10 12:09

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

    0 讨论(0)
提交回复
热议问题