How to Create a Uiimage from the Current Graphics Context

How to create a UIImage from the current graphics context?

You need to setup the graphics context first:

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

How to draw an UIImage using drawAtPoint after translating the image graphics context

I am seeing that if I apply a CGContextTranslateCTM to the image graphics context, the image is not drawn

Of course. You draw at 0,0. But first you apply a translate CTM of 100,100. So you actually draw at 100,100. But the context is only 20 points wide. So you are drawing completely outside the context. There is no context outside the context, so nothing is drawn.

Draw Rectangle On UIImage with Core Graphics

I feel as if it's not drawing on top of the image which I sent in,
it's creating a new image altogether.

No feelings involved. That's literally what this code does.

  1. Creates a new context
  2. Draws a rectangle into it
  3. Makes an image out of the context, and returns it

Between steps 1 and 2, you need to draw your original image into the context.

Also, there is no need to set the line width or stroke color, since you are only filling the rectangle, not stroking it. (If you are seeing red for some reason, it wasn't because of this code; do you have a different view or image that has a red background?)

func drawRectangleOnImage(image: UIImage) -> UIImage {
let imageSize = image.size
let scale: CGFloat = 0
UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)
let context = UIGraphicsGetCurrentContext()

image.draw(at: CGPoint.zero)

let rectangle = CGRect(x: 0, y: (imageSize.height/2) - 30, width: imageSize.width, height: 60)

CGContextSetFillColorWithColor(context, UIColor.black.CGColor)
CGContextAddRect(context, rectangle)
CGContextDrawPath(context, .Fill)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}

And you could further simplify by using higher-level UIKit API to draw.

func drawRectangleOnImage(image: UIImage) -> UIImage {
let imageSize = image.size
let scale: CGFloat = 0
UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)

image.draw(at: CGPoint.zero)

let rectangle = CGRect(x: 0, y: (imageSize.height/2) - 30, width: imageSize.width, height: 60)

UIColor.black.setFill()
UIRectFill(rectangle)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}

Swift 3 draw uiimage programmatically

Here is example you can have first image if you set isWhole property to false and have second image if you set it to true. You can paste this code in viewDidLoad to test and play with it.

    var isWhole = false
UIGraphicsBeginImageContextWithOptions(CGSize.init(width: 27, height: 5), false,0.0)
var context = UIGraphicsGetCurrentContext()
context?.setFillColor(UIColor.red.cgColor)
if(context != nil){
if(isWhole){
context?.fill(CGRect(x: 0, y: 0, width: 27, height: 2.5))
}
else{
context?.fill(CGRect(x: 0, y: 0, width: 13.5, height: 2.5))
}
context?.setFillColor(UIColor.gray.cgColor)
context?.fill(CGRect(x: 0, y: 2.5, width: 27, height: 2.5))
}

let newImage = UIGraphicsGetImageFromCurrentImageContext()
var imageView = UIImageView(frame: CGRect(x: 100, y: 200, width: 27, height: 5))
imageView.image = newImage
self.view.addSubview(imageView)
UIGraphicsEndImageContext()

If you need your red rectangle to be with rounder corners just change fill(rect:CGRect) with path like this:

    if(isWhole){
context?.addPath(CGPath(roundedRect: CGRect(x: 0, y: 0, width: 27, height: 2.5), cornerWidth: 1, cornerHeight: 1, transform: nil))
context?.fillPath()
}

Create UIImage of a specific size

Here is an answer for you stitch picture in iphone

And base on my experience, I can give you some note:

Propotional scale


- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize);
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}

Resize


- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}

Handle specific view

You hava to import QuzrtzCore.framework first


-(UIImage*)captureView:(UIView *)theView
{
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

Handle a range form image


CGRect captureRect = yourRect
CGRect viewRect = self.view.frame;
UIImage *viewImg;
UIImage *captureImg;

UIGraphicsBeginImageContext(viewRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
viewImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

captureImg = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(viewImg.CGImage, captureRect)];

Save the image

Save in app


NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

Save in album


CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

How do I draw on an image in Swift?

It's simple:

  1. Make an image graphics context. (Before iOS 10, you would do this by calling UIGraphicsBeginImageContextWithOptions. In iOS 10 there's another way, UIGraphicsImageRenderer, but you don't have to use it if you don't want to.)

  2. Draw (i.e. copy) the image into the context. (UIImage actually has draw... methods for this very purpose.)

  3. Draw your line into the context. (There are CGContext functions for this.)

  4. Extract the resulting image from the context. (For example, if you used UIGraphicsBeginImageContextWithOptions, you would use UIGraphicsGetImageFromCurrentImageContext.) Then close the context.

create CGImage from UIView Subclass

-renderInContext: is a method on CALayer, not UIView.

Do this instead:

[myView.layer renderInContext:UIGraphicsGetCurrentContext()];

You will also need to import the headers for CALayer:

 #import <QuartzCore/QuartzCore.h>

And link your app against the QuartzCore framework.

iOS current graphics context - What is that

The OS needs a place to save information, such as drawing state, which you don't want to specify in every single CG drawing command, such as in which bitmap or view to draw, the scale or other transform to use, the last color you specified, etc.

The context tells each CG call where to find all this "stuff" for your current drawing call. Give a different context to the exact same drawing call, and that call might draw to a different bitmap in a completely different view, with a different color, different scale, etc.

drawing UIImage inside a CGMutablePath

When you say "draw within a CGMutablePath" I have to assume you mean "clip to a CGMutablePath." In that case, you just need to add the path to the current context (CGContextAddPath() or any of the path-building functions like CGContextAddLineToPoint()) and then call CGContextClip(). You can then draw your image in the context with something like drawAtPoint: or CGContextDrawImage().



Related Topics



Leave a reply



Submit