UIBezierPath : Incorrect retrieval of view screenshot - iphone

I'm taking the screenshot following way.
- (UIImage*)screenshot
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
On UIView I'm drawing paths using UIBezierPath.
The screenshots that I'm getting are incorrect like this
Any ideas why the upper part is cut to blank ? On UIView all drawing is displayed correctly.
UPDATE : This happens when I draw a long path with UIBezierPath when I release the my brush and get the screenshot it gets the screenshot correctly.

I saw the another thread which you post.
According to the apple technical Q&A, you need to adjust the geometry coordinate first.
So, before render the layer of window, do following things first.
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);

- (UIImage*)screenshot :(UIView*)vw
{
CGRect rect = [vw bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[vw.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Use This one

-(IBAction)takeScreenShot:(id)sender
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

Related

image is blurred after cropping the image?

I have an application in which i am cropping the image taken from the camera.all are going well.but after the cropping the image seems to blured and streched.
CGRect rect = CGRectMake(20,40,280,200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y,280,200);
// 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
[image drawInRect:drawRect];
// grab image
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize size = [croppedImage size];
NSLog(#" = %#",NSStringFromCGSize(size));
NSData* pictureData = UIImagePNGRepresentation(croppedImage);
Can anybody help me in finding out where i am going wrong?
try replacing
UIGraphicsBeginImageContext(rect.size);
with
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
to account for retina

Remove Top View From Hierarchy When Saving Screenshot

I am using a method to take a UIImage screenshot programatically which works very nicely. What I want to do now is remove the top most UIView from being included in the screenshot BUT not remove it from the actual view on screen so simply setting topView.hidden = YES won't do. I am using this method to save the screenshot:
- (UIImage*)screenshot
{
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if (![window respondsToSelector:#selector(screen)] || [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[window layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
}
// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
My first thought was to remove the sublayer from the superlayer doing this:
NSArray *sublayers = [[window layer] sublayers];
[[window layer] replaceSublayer:[sublayers objectAtIndex:[sublayers count] - 1] with:nil];
but I found that the sublayers array is only 1 object. How could I remove the top view from the screenshot?
For example you have View1 on your view hierarchy and you don't want to take screenshot of View1 then you can do View1.hiden= TRUE; before screenshot and you can do View1.hiden= FALSE; after screenshot.
May be this will help you.

Resolution of capture screen image is not retina

I am using the following code for capture a view on the screen but it is not as sharp as on the screen. the imageView size is 200x200point but the scale is 2.0 (with retina screen). The saved img size is 200x200 px. how could i make the img as sharp as the original one? Any help will be appreciated!
- (UIImage*)captureView:(UIView *)theView {
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Try like that:
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
You are using the old way of creating the image context. Use this instead:
UIGraphicsBeginImageContextWithOptions(rect.size, YES, [UIScreen mainScreen].scale);
The older UIGraphicsBeginImageContext function always assumes a scale of 1.0.
Replace 'YES' with 'NO' if you need the alpha channel.

How to save the UIImage after multi point cropping the image?

I want to multi point crop on the image(See the image). Its working fine. My problem is after crop the image the how to I save UIImage. I am using the CAShapeLayer for crop image. Below code using for multi point crop.
- (void)multiPointCrop:(CGPoint)cropPoint
{
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:cropPoint];
for (NSString *pointString in self.touchPoints) {
if ([self.touchPoints indexOfObject:pointString] != 0)
[aPath addLineToPoint:CGPointFromString(pointString)];
}
[aPath addLineToPoint:cropPoint];
[aPath closePath];
[self setClippingPath:aPath andView:self];
[self setNeedsDisplay];
}
- (void)setClippingPath:(UIBezierPath *)clippingPath andView:(UIView *)view;
{
if (![[view layer] mask])
[[view layer] setMask:[CAShapeLayer layer]];
[(CAShapeLayer*) [[view layer] mask] setPath:[clippingPath CGPath]];
}
How to I save UIImage from the CAShapeLayer? If this is the correct way for multi cropping or any other easy way to achieve this. Please give your ideas, suggestions, source code, etc. Anything always welcome.
Try rendering layer into a context and creating an image from that context.
CALayer *layer = view.layer;
CGSize s = layer.frame.size;
// create context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, s.width, s.height,
8, (s.width * 4),
colorSpace, kCGImageAlphaPremultipliedLast);
// flip Y
CGContextTranslateCTM(context, 0.0, s.height);
CGContextScaleCTM(context, 1.0, -1.0);
// render layer
[layer renderInContext:context];
CGImageRef imgRef = CGBitmapContextCreateImage(context);
// here is your image
UIImage *img = [UIImage imageWithCGImage:imgRef];
// release owned memory
CGImageRelease(imgRef);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);

How to add a overlay view to the cameraview and save it

Thanks in advance.
I want to add an image view as a overlay view to the camera and save both the (cameraview and image view )as a single image. I searched for it and tried the example given in stackoverflow but it is not displaying any thing than blank screen .If any one know please help me.
- (void)renderView:(UIView*)view inContext:(CGContextRef)context
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [view center].x, [view center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [view transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[view bounds].size.width * [[view layer] anchorPoint].x,
-[view bounds].size.height * [[view layer] anchorPoint].y);
// Render the layer hierarchy to the current context
[[view layer] renderInContext:context];
// Restore the context
CGContextRestoreGState(context);
}
// this get called when an image has been taken from the camera
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//cameraImage = image;
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw the image returned by the camera sample buffer into the context.
// Draw it into the same sized rectangle as the view that is displayed on the screen.
float menubarUIOffset = 44.0;
UIGraphicsPushContext(context);
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height-menubarUIOffset)];
UIGraphicsPopContext();
// Render the camera overlay view into the graphic context that we created above.
[self renderView:self.imagePicker.view inContext:context];
// Retrieve the screenshot image containing both the camera content and the overlay view
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
UIGraphicsEndImageContext();
[self.imagePicker dismissModalViewControllerAnimated:YES];
}
Hope this helps