i am trying to merge two different images in single image and trying to save it in the photo gallery. But the sticker which i am trying to merge on the image is getting stretched and not properly align to x and y in the image.
the code i am using is as below:
- (UIImage*)buildImage:(UIImage*)image
{
UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
[image drawAtPoint:CGPointZero];
CGFloat scale = image.size.width / _workingView.width;
NSLog(#"%f",scale);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
[_workingView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tmp;
}
here in this code _workingview is the view containing the sticker image inside it & image in the arguement is the main image which we are passing in the function
i would be very much thank full for your help
Thanks Guys
CGFloat width, height;
CGFloat width, height;
UIImage *inputImage; // input image to be composited over new image as example
// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0);
// get context
CGContextRef context = UIGraphicsGetCurrentContext();
// push context to make it current
// (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);
// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
[inputImage drawInRect:CGRectMake(0, 0, width, height)];
[stickerImage drawInRect:CGRectMake(yourX,yourY,othertWidth,otherHeight)]
// pop context
UIGraphicsPopContext();
// get a UIImage from the image context- enjoy!!!
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
// clean up drawing environment
UIGraphicsEndImageContext();
Related
I want to crop a 200*200 image, the cropped part is (x=10,y=10,w=50,h=50). and drawing this part to new 500*500 image, the new rect is (x=30,y=30,w=50, h=50), how to do it?
I can get the part of image with the following method
- (UIImage*) getSubImageWithRect: (CGRect) rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.size.width, self.size.height);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
[self drawInRect:drawRect];
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
Lets try using following code chunk
- (UIImage*) getSubImageFromImage:(UIImage *)image
{
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(10, 10, 50, 50);
// Create Image Ref on Image
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
// Get Cropped Image
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
I want to fill image with percentage (e.g. same as rating).
Actually am showing rating image with with float value.
Depend upon float value i need to fill the with particular image.
I got result to fill over all image. But my query is to fill image with percentage value.
My code:
- (void)viewDidLoad
{
[super viewDidLoad];
_ColImgView.image = [self imageNamed:#"star.png" withColor:[UIColor redColor]];
}
- (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
UIImage *img = [UIImage imageNamed:name];
// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(img.size);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
[color setFill];
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);
// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, img.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return coloredImg;
}
Preview image:
You have to add overlay. Overlay should be under image and image should be png with transparency. To fill overlay with color in specified area you can use code from here:
iOS - Fill bezierPath with multiple colors based on percentage
If I will find free time I will write code for you, but for now I can only explain what you should do.
I am not able to draw image on top of another image. Below if the code I am using:
-(void)createImage
{
// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(imgVw_noHair_.frame.size, YES, 0.0);
// get context
CGContextRef context = UIGraphicsGetCurrentContext();
// push context to make it current
// (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);
// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
[eraser drawAtPoint:CGPointMake(50, 50)];
// pop context
UIGraphicsPopContext();
UIImage _image_ = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
The image which I get from the current context is a black image. I want to draw eraser image on the image which I get from the context.
This is updated code which is working in my side.
-(void)createImage
{
//Create context in which you have to draw
UIGraphicsBeginImageContextWithOptions(imgvwImage.image.size, YES, 0.0);
// get context
CGContextRef context = UIGraphicsGetCurrentContext();
// push context to make it current
// (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);
//draw the old image in that context
[imgvwImage.image drawInRect:CGRectMake(0, 0, 200, 200)];
UIImage *img=[UIImage imageNamed:#"img.png"];
//Draw your image in that context
[img drawAtPoint:CGPointMake(50, 50)];
// pop context
UIGraphicsPopContext();
UIImage *image_ = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Get that image
imgvwImage.image=image_;
}
try to draw your eraser image in rect instead of AtPoint
[eraser drawInRect:CGRectMake(50, 50, width, height)];
You forgot to draw the bottom image into the context, hence you are drawing your eraser image on top of an uninitialized (black) background. You need to add some code to first draw your desired background image.
I 've a PNG loaded into an UIImage. I want to get a portion of the image based on a path (i.e. it might not be a rectangular). Say, it might be some shape with arcs, etc. Like a drawing path.
What would be the easiest way to do that?
Thanks.
I haven't run this, so it may not be perfect but this should give you an idea.
UIImage *imageToClip = //get your image somehow
CGPathRef yourPath = //get your path somehow
CGImageRef imageRef = [imageToClip CGImage];
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0, CGImageGetColorSpace(imageRef), kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextAddPath(context, yourPath);
CGContextClip(context);
CGImageRef clippedImageRef = CGBitmapContextCreateImage(context);
UIImage *clippedImage = [UIImage imageWithCGImage:clippedImageRef];//your final, masked image
CGImageRelease(clippedImageRef);
CGContextRelease(context);
The easiest way to add a category to the UIImage with follow method:
-(UIImage *)scaleToRect:(CGRect)rect{
// Create a bitmap graphics context
// This will also set it as the current context
UIGraphicsBeginImageContext(size);
// Draw the scaled image in the current context
[self drawInRect:rect];
// Create a new image from current context
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// Pop the current context from the stack
UIGraphicsEndImageContext();
// Return our new scaled image
return scaledImage;
}
I have a UIImage which is an image of a mountainous landscape. I need to show locations on this image, by turning corresponding 2-3 pixels at the spot to red. How do I accomplish this? Thanks!
You can draw image to graphics context like this:
CGRect imageRect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
//Save current status of graphics context
CGContextSaveGState(context);
CGContextDrawImage(context, imageRect, image.CGImage);
And then just draw a point on it wherever you want like this:
//CGContextFillRect(context, CGRectMake(x,y,1,1));
//Fix error according to #gsempe's comment
CGContextFillRect(context, CGRectMake(x,y,1./(image.scale),1./(image.scale)))
Then just save it to UIImage again:
CGContextRestoreGState(context);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
You also should take care of image's orientation. Here is some good article on it.