How to generate PDF of current view - ipad - ipad

I am trying to create a PDF file of current view. Can anyone please tell me about a tutorial link where I can find how to create PDF file of whole current view?
Thanks.

First save the current view as an image with this function:
- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect imageRect = CGRectMake(0, 0, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect));
UIGraphicsBeginImageContext(imageRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextTranslateCTM(ctx, 0, 0);
CGContextFillRect(ctx, imageRect);
[webView.layer renderInContext:ctx];
UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);
UIGraphicsEndImageContext();
// alert to let us know success. remove this later.
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:#"Image Saved" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
Next convert the image to PDF by following the directions in this StackOverflow question:
How can i create a PDF file programmatically in an iphone application?

Related

How to programmatically take a screenshot in iOS?

How can I programmatically take a screenshot of the entire screen (iPhone) and save it to the photo library in iOS? There is nothing on the screen other than some labels and a button.
The button is named "ScreenshotButton" that needs to trigger this.
Do I still need to import QuartzCore?
Where exactly would I need to place the Screenshot Function in the ViewController?
You need CoreGraphics. Here's the code I use within the IBAction of a button:
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize imageSize = rect.size;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextConcatCTM(ctx, [self.view.layer affineTransform]);
if ([self.view respondsToSelector:#selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { // iOS 7+
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
} else { // iOS 6
[self.view.layer renderInContext:ctx];
}
screengrab = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(ctx);
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(screengrab, nil, nil, nil);
I define static UIImage *screengrab; at the top of my code after #implementation.
You should use Analyze to check for leaks -- I don't have any myself in this code, but CG code always seems to create some.
Hereunder the class used to take snapshot of a view :
#import "ScreenSnapshot.h"
#import <QuartzCore/QuartzCore.h>
#implementation ScreenSnapshot
static NSUInteger imgNumber;
+ (void)initialize { imgNumber = 0; }
+ (void)saveScreenSnapshot:(UIView *)view {
UIImage * img = [self screenSnapshotOf:view];
NSData * data = UIImagePNGRepresentation(img);
imgNumber += 1;
NSString * fullPath = [NSString stringWithFormat:#"%#/Documents/img_%ld.png",NSHomeDirectory(),imgNumber];
[data writeToFile:fullPath
atomically:YES];
}
+ (UIImage *)screenSnapshotOf:(UIView *)view {
UIGraphicsBeginImageContext(view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
#end
Copy this code as is in a new class called 'ScreenSnapshot'.
In your "ScreenshotButton" action, write:
[ScreenSnapshot saveScreenSnapshot:self.view];
...after having imported "ScreenSnapshot.h".
You can replace the location used in my code to store the images and you'll be able to programmatically take a screenshot in iOS.

Merging multiple image into single image

I want to merge multiple images into single image while saving to camera roll.
Actually,I'm having different images in different views.I'm having 3 images in one view and also 4 images when selecting layout with 4 frames.someone help me how to merge them.
my sharing code:
CGRect rect=[self.previewView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGSize size=CGSizeMake(self.previewView.frame.size.width, self.previewView.frame.size.height);
if(selectButtonTag==2)
{
[self.doubleFrameViewcapturedImg1.image drawInRect:CGRectMake(0, 0, size.width, size.height)blendMode:kCGBlendModeNormal alpha:0.5];
[self.doubleFrameViewcapturedImg2.image drawInRect:CGRectMake(0,0,size.width,size.height) blendMode:kCGBlendModeNormal alpha:0.5];
}
CGContextRef context=UIGraphicsGetCurrentContext();
[self.previewView.layer renderInContext:context];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSArray *activity=[[NSArray alloc]initWithObjects:UIActivityTypeCopyToPasteboard,
UIActivityTypeMail,
UIActivityTypeMessage,
UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAirDrop
, nil];
UIActivityViewController *ShareView=[[UIActivityViewController alloc]initWithActivityItems:#[newImage,activity] applicationActivities:nil];
ShareView.popoverPresentationController.sourceView=self.view;
[self presentViewController:ShareView animated:YES completion:nil];
you can do this by using CoreGraphics,for more details just follow the tutorial by CodeTutePlus AdvancedImageTechnicsCoreGraphics

Screen capture for iOS [duplicate]

This question already has answers here:
iOS Screenshot part of the screen
(4 answers)
Closed 8 years ago.
i m using this codes for screen capture for iOS:
CGImageRef screen = UIGetScreenImage();
UIImage *image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
CGRect screenshotFrame;
CGImageRef screenshotRef = CGImageCreateWithImageInRect(screenshotRef, screenshotFrame);
UIImage * screenshot = [[(UIImage *)screenshotRef retain] autorelease];
CGImageRelease(screenshotRef);
But my screen contain tool bar bottom of the screen.i don't want tool bar on my captured screen. How can i crop tool bar ? How can i modified my code ?
I think you can capture the layer of self.view because it the root view
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
//Retina display
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(self.view.bounds.size);
}
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Have fun!
Try this:
UIGraphicsBeginImageContext(/*CGRECT*/);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Here in CGRect pass your rect which you want to take screenshot.
Hope it helps you..
Set your
CGRect screenshotFrame = CGRectMake(0, 0, 320, 436);
// 480(screen height) - 44(toolbar height) = 436

Taking Screen Shot When App is in Landscape Mode

I am working on an app which will only be in landscape. In that app I have a functionality that is take screenshot of that particular screen. I have implemented this code
UIGraphicsBeginImageContext(self.view.window.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();;
imageView.transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0);
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(imageView.image, nil, nil, nil);
Using this code I get screen shot of my app in portrait mode. I want it in landscape mode.
Don't add that window. If you add it your context size is wrong.
// Just use self.view.bounds.size is OK
UIGraphicsBeginImageContext(self.view.bounds.size);
Perhaps late, but wrapping it in an UIImageView doesn't actually rotate the image itself.
Here's some code I wrote that creates a rotated image from the full window of your application.
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [[UIImage alloc] initWithCGImage:[image CGImage] scale:1 orientation:UIImageOrientationLeft];
If you want it rotated right, just replace UIImageOrientationLeft with UIImageOrientationRight

cordova screenshot plugin cant return image

I am building an iPhone/iPad app using Cordova/jQuery Mobile. Ive used the following screenshot plugin https://github.com/josemando/phonegap-plugins/commit/4c2ec54ae001ad409e4d4fe6f5ec6b9ef8b7dc3f which works a treat. It captures a screenshot and saves it to my Camera Roll Album. However how can i then get a hold of this image in my Javascript to eventually upload it onto my web server.
I use the following js script to execute the screenshot function, but it doesnt return an image.
Screenshot.prototype.saveScreenshot = function() {
cordovaRef.exec("Screenshot.saveScreenshot");
};
This is the Screenshot.m file:
#import "Screenshot.h"
#implementation Screenshot
#synthesize webView;
- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
{
CGRect imageRect;
CGRect screenRect = [[UIScreen mainScreen] bounds];
// statusBarOrientation is more reliable than UIDevice.orientation
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
// landscape check
imageRect = CGRectMake(0, 0, CGRectGetHeight(screenRect), CGRectGetWidth(screenRect));
} else {
// portrait check
imageRect = CGRectMake(0, 0, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect));
}
UIGraphicsBeginImageContext(imageRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextTranslateCTM(ctx, 0, 0);
CGContextFillRect(ctx, imageRect);
[webView.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
UIGraphicsEndImageContext();
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:#"Image Saved" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
#end
I don't think that this could work!
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
as Example:
UIImageWriteToSavedPhotosAlbum(theImage,
self, // send the message to 'self' when calling the callback
#selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), // the selector to tell the method to call on completion
NULL); // you generally won't need a contextInfo here
look here
to get an image
- (UIImage*)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
....
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
....
UIGraphicsEndImageContext();
....
return image;

Resources