Take screenshot from UIVIEW+ipad - iphone

I want to take the screenhot of viewcontroller
I had write this:-
- (UIImage *)captureScreenInRectWOScroll:(CGRect)captureFrame {
CALayer *layer;
layer = self.view.layer;
UIGraphicsBeginImageContext(captureFrame.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}
The way i am calling this method:-
UIImage *img_woscroll1 =[self captureScreenInRectWOScroll:CGRectMake(35,147,497,260)];
I wann to take screenshot of Address from below attached image:-
When i am taking hte screenshot from the above code i Got the image with lot of blank space(greencolor image at top) at top side if image:-
Please help me..How to take proper screenshot from image such that i will not get blank image on topside.

Try This
UIGraphicsBeginImageContext(myView.frame.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data=UIImagePNGRepresentation(viewImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strPath = [documentsDirectory stringByAppendingPathComponent:#"myimage.png"];
[data writeToFile:strPath atomically:YES];

Try This:-
CGRect screenRect = CGRectMake(0, 90, 320,460);
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextFillRect(ctx, screenRect);
//you probably need an offset, adjust here
CGContextTranslateCTM(ctx, -20, -20);
[self.view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
It may help you. Thanks :)

Try with this. This may help you.
- (UIImage *)captureView:(UIView *)view {
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Related

Low memory waning while taking screenshot of a view in iPhone

I am having an app in which I am taking a screenshot of a view and saving that image on documents folder.
I am using the following code.
CGSize size = self.view.bounds.size;
CGRect cropRect;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if([self isPad])
{
cropRect = CGRectMake(145, 110, 476, 476);
}
else
{
if (screenBounds.size.height ==568)
{
cropRect = CGRectMake(40, 69, 240, 240);
}
else
{
cropRect = CGRectMake(40, 62, 240, 240);
}
}
/* Get the entire on screen map as Image */
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Crop the desired region */
CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
UIImage * cropImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
/* Save the cropped image
UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);*/
//save to document folder
NSData * imageData = UIImageJPEGRepresentation(cropImage, 1.0);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
imagename=[NSString stringWithFormat:#"Fff.jpg"];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imagename];
////NSLog(#"full path %#",fullPathToFile);
[imageData writeToFile:fullPathToFile atomically:NO];
It works fine if I take the screenshot 15 to 20 times but after that It gives me low memory warning and the app crashes after that on this code.
Is there a more optimized code that I can use which does not cause such memory problems.
Please help me.
Capture screen with my bellow method..
- (UIImage *)captureView {
//hide controls if needed
CGRect rect = [self.view bounds];// Here define CGRect with your requirement of take screenshot of some part
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
See my another answer howe-to-capture-uiview-top-uiview

Printing and saving a web page Using iPhone

I want my WebView to save as image or pdf of any formate,
I tried with saving the web page using the code :
UIGraphicsBeginImageContext(WebPage.frame.size);
[WebPage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
but i'm getting the part which is visible..
i want to know how to get the entire web page image or how the apple people giving the air print so that the entire web page can b printed.. i dont want to know the "AirPrint function" i want to know how to get web page image using the iPhone..
As i'm fresher to iOS development.
can any one help me with he working code of saving web page?
- (void)printAndSave
{
webViewHeight = [[self.myWebView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"] integerValue];
CGRect screenRect = self.myWebView.frame;
double currentWebViewHeight = webViewHeight;
while (currentWebViewHeight > 0)
{
imageName ++;
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.myWebView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.png",imageName]];
if(currentWebViewHeight < 460)
{
CGRect lastImageRect = CGRectMake(0, 457 - currentWebViewHeight, self.myWebView.frame.size.width, currentWebViewHeight);
CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
newImage = [UIImage imageWithCGImage:lastImageRef];
CGImageRelease(lastImageRef);
}
[UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
[self.myWebView stringByEvaluatingJavaScriptFromString:#"window.scrollBy(0,460);"];
currentWebViewHeight -= 460;
}
}
- (IBAction)printSaveTheWebView:(id)sender
{
UIImage *viewImage;
UIScrollView *Scroll_view = webView.scrollView;
CGRect savedFrame;
UIGraphicsBeginImageContext(Scroll_view.contentSize);
{
CGPoint savedContentOffset = Scroll_view.contentOffset;
savedFrame = Scroll_view.frame;
Scroll_view.contentOffset = CGPointZero;
Scroll_view.frame = CGRectMake(0, 0, Scroll_view.contentSize.width, Scroll_view.contentSize.height);
[Scroll_view.layer renderInContext: UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
Scroll_view.contentOffset = savedContentOffset;
Scroll_view.frame = savedFrame;
}
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
[UIImagePNGRepresentation(viewImage) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:#"view.png"] atomically:YES];
}
Call this method when you click your button
For getting your reqirement you have to change the context of the webview by changing the frame ,for that take one for loop and loop it according to the number of pages by changing the contex.
once check this one

How can I export UITableViewCell into image?

I need to export UITableViewCell into an image PNG or anything so I can send it as a print screen image embedded in email body using the MFMailComposerViewController.
Use these methods. Just call UIImage *image = [cell screenshot];
- (UIImage *)screenshot
{
return [self screenshotForRect:self.bounds];
}
- (UIImage *)screenshotForRect:(CGRect) rect
{
UIGraphicsBeginImageContext(rect.size);
[[UIColor clearColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.layer renderInContext:ctx];
UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return anImage;
}
- (UIImage *)captureCell {
//hide controls if needed
CGRect rect = [yourTableCell bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[yourTableCell.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
try something like that:
UIGraphicsBeginImageContext(yourTableViewCellView.bounds.size);
[yourTableViewCellView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
and then you can save the image to data :)

Rotated images having jagged borders even after adding transparent border

In my app I need to rotate images.I rotated the images but they have jagged borders.I googled and found that adding transparent border solves the problem, but the borders are still jagged.
My code is
-(void)CreateRotatedImage:(UIImage *)image Number:(int)Number
{
UIImage *ImageBackground=[UIImage imageNamed:#"white-highlight.png"];
CGSize ImageBackgroundSize = CGSizeMake(60, 50);
UIGraphicsBeginImageContext(ImageBackgroundSize);
CGRect ImageBackgroundRect = CGRectMake(0.0, 0.0, ImageBackgroundSize.width, ImageBackgroundSize.height);
[ImageBackground drawInRect:ImageBackgroundRect];
ImageBackground = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize ImageSize = CGSizeMake(50, 40);
UIGraphicsBeginImageContext(ImageSize);
CGRect ImageRect = CGRectMake(0.0, 0.0, ImageSize.width, ImageSize.height);
[image drawInRect:ImageRect];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(CGSizeMake(60,50));
[ImageBackground drawAtPoint:CGPointMake(0,0)];
[image drawAtPoint:CGPointMake(5,5)];
// MyscrollView.backgroundColor = [UIColor clearColor];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *pin=[UIImage imageNamed:#"pin.png"];
UIGraphicsBeginImageContext(CGSizeMake(60,50));
[newImage drawAtPoint:CGPointMake(0,0)];
[pin drawAtPoint:CGPointMake(newImage.size.height/2,0)];
// MyscrollView.backgroundColor = [UIColor clearColor];
UIImage *IntermediateImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect imageRrect = CGRectMake(0, 0, IntermediateImage.size.width, IntermediateImage.size.height);
UIGraphicsBeginImageContext( imageRrect.size );
[IntermediateImage drawInRect:CGRectMake(2,2,IntermediateImage.size.width-2,IntermediateImage.size.height-2)];
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize iSize = CGSizeMake(110, 70);
UIGraphicsBeginImageContext(iSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES);
CGContextRotateCTM (context, radians(5));
[i drawAtPoint:CGPointMake(0, 0)];
UIImage *RotatedImage=UIGraphicsGetImageFromCurrentImageContext();
//RotatedImage=[RotatedImage resizedImage:iSize interpolationQuality:kCGInterpolationHigh];
}

Capturing Screen

I am trying to capture (screen shot) a view. For that I am using a piece of code shown below that saves it to my document directory as a PNG image.
UIGraphicsBeginImageContextWithOptions(highlightViewController.fhView.centerView.frame.size, YES, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"1.png"];
NSData *imageData = UIImagePNGRepresentation(screenshot);
[imageData writeToFile:appFile atomically:YES];
UIGraphicsEndImageContext();
Question: can I capture part of the view? Because in the above code I can't change the origin (frame). If anyone has other approach to capture a particular part of view please share it.
You could crop the image:
http://iosdevelopertips.com/graphics/how-to-crop-an-image.html
CGRect rect = CGRectMake(0,0,10,10);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
Try this code. This surely works as I have implemented it in many of my projects:
- (UIImage *)image
{
if (cachedImage == nil) {
//YOU CAN CHANGE THE FRAME HERE TO WHATEVER YOU WANT TO CAPTURE
CGRect imageFrame = CGRectMake(0, 0, 400, 300);
UIView *imageView = [[UIView alloc] initWithFrame:imageFrame];
[imageView setOpaque:YES];
[imageView setUserInteractionEnabled:NO];
[self renderInView:imageView withTheme:nil];
UIGraphicsBeginImageContext(imageView.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextGetCTM(c);
CGContextScaleCTM(c, 1, -1);
CGContextTranslateCTM(c, 0, -imageView.bounds.size.height);
[imageView.layer renderInContext:c];
cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
// rescale graph
UIImage* bigImage = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef scaledImage = [self newCGImageFromImage:[bigImage CGImage] scaledToSize:CGSizeMake(100.0f, 75.0f)];
cachedImage = [[UIImage imageWithCGImage:scaledImage] retain];
CGImageRelease(scaledImage);
UIGraphicsEndImageContext();
[imageView release];
}
return cachedImage;
}
I hope this will help you.
See if you can specify the rect like this and then take screenshot.
CGRect requiredRect = CGRectMake(urView.frame.origin.x, urView.frame.origin.y, urView.bounds.size.width, urView.bounds.size.height);
UIGraphicsBeginImageContext(requiredRect.size);
You can alter the origin and see if it works.
If this doesn't work out, you can try cropping the image as mentioned by #mcb
You can use this code
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect;
rect = CGRectMake(250,61 ,410, 255);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
CGImageRelease(imageRef);