Get the image of Web Page - iphone

I'm trying to capture the full image of the web page..
But its showing the blank image..
what should i do to get the complete Web Page Image?
I have tried with
CGSize overallSize = [WebPageImage sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(WebPageImage.scrollView.contentSize);
WebPageImage.bounds = CGRectMake(0, 0, overallSize.width, overallSize.height);
while (WebPageImage.loading) {
[NSThread sleepForTimeInterval:0.1];
}
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,(unsigned long)NULL), ^(void) {
UIImageWriteToSavedPhotosAlbum(viewImage, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
});
Please suggest some solution..
WebPageImage is UIWebView..

UIGraphicsBeginImageContextWithOptions(WebPageImage.bounds.size, WebPageImage.opaque, 0.0);
[WebPageImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

use this function according to your need. below code will give you snapshot of whole webpage
- (UIImage *) imageFromWebView:(UIWebView *)view
{
// tempframe to reset view size after image was created
CGRect tmpFrame = view.frame;
// set new Frame
CGRect aFrame = view.frame;
aFrame.size.height = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
aFrame.size.width = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].width;
view.frame = aFrame;
// do image magic
UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);
CGContextRef resizedContext = UIGraphicsGetCurrentContext();
[self.thmbWebView.layer renderInContext:resizedContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// reset Frame of view to origin
view.frame = tmpFrame;
return image;
}

Try this:
[WebPageImage.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();

try to capture that image with my bellow method...
-(IBAction)captureScreen:(id)sender
{
UIGraphicsBeginImageContext(webview.frame.size);
[webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
Also see my another answer from this link.. howe-to-capture-uiview-top-uiview

Related

how to convert the part of UIView into UIImage and display it in imageView

I have a view in which i am drawing the signature i want that the signature part of the view should be converted as UIImage and then display it in UIImageView here is the code which i got from net i am using for converting
UIGraphicsBeginImageContext(signatureView.bounds.size);
[signatureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
logoImageView.image=img;
UIGraphicsEndImageContext();
Try this hope will help you.
- (UIImage *) getUIImageWithmyView:(UImyView *)myView
{
UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, 0.0);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return myImage;
}
use my bellow method...
- (UIImage *)captureView {
//hide controls if needed
CGRect rect = [signetureView bounds];//use your signature view's Rect means Frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
you can call this above method like bellow...
UIImage *tempImageSave=[self captureView];
try this:
UIGraphicsBeginImageContext(signatureView.bounds.size);
[signatureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
logoImageView.image=img;
first endimagecontext and after that set/use that image in imageview

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 :)

how to take a screen shot from a mapview

In my application i have used mapview. On particular place i can put annotation. i want to take the snap shot of that place with annotation in it.
i tried to implement this.
CGSize size = self.view.bounds.size;
CGRect screensize = CGRectMake(40,40,size.width-240,size.height-400);
UIGraphicsBeginImageContext(screensize.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
UIGraphicsEndImageContext();
this code takes the snap but it takes the snap from (0,0) coordinates. i want to take the snap from (40,40) coordinates and the size of the image should be 80*80.
How can i do this?
I don't think you can do it directly. Get the entire image and the crop the desired region.
CGSize size = self.view.bounds.size;
CGRect cropRect = CGRectMake(40, 40, 80, 80);
/* 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);
UIGraphicsEndImageContext();

Capturing of Screen shot starting from 0,0

I am using following code to make a screen shot
UIGraphicsBeginImageContext(self.view.frame.size);
blendMode:kCGBlendModeClear alpha:1.0];
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
return viewImage;
It is working fine but it returns the full screen, and I want a screen shot of a particular Frame, like ( 100,100,200,200), I tried to make changes in:
UIGraphicsBeginImageContext(self.view.frame.size);
But no success.
Try this:
float x = 100;
float y = 100;
CGSize size = CGSizeMake(200,200);
UIGraphicsBeginImageContext(size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, -x, -y);
[view.layer renderInContext:c];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
just use three line of code.
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *frame= UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsBeginImageContext with parameters

I am taking a screenshot in my application. I am able to take the screenshot.
Now I want to take the screenshot by specifying the x and y coordinate. Is that possible?
UIGraphicsBeginImageContext( self.view.bounds.size );
[self.view.layer renderInContext:UIGraphicsGetCurrentContext( )];
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext( );
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
If you're using a newer retina display device, your code should factor in the resolution by using UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 0, -40); // <-- shift everything up by 40px when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This will render the retina display image context.
Just do something like this, way easier than all those complex calculations
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
Here is Swift version
//Capture Screen
func capture()->UIImage {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, UIScreen.mainScreen().scale)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
swift version
UIGraphicsBeginImageContext(self.view.bounds.size)
let image: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextTranslateCTM(image, 0, -40)
// <-- shift everything up by 40px when drawing.
self.view.layer.renderInContext(image)
let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)