I will convert a PDF with sometimes more then one pages to an PNG.
Actually I use this Code:
NSURL* url = [NSURL fileURLWithPath: pdfPath];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);
UIGraphicsBeginImageContext(CGSizeMake(596,842));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0, 842);
CGContextScaleCTM(currentContext, 1.0, -1.0); // make sure the page is the right way up
CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // first page of PDF is page 1 (not zero)
CGContextDrawPDFPage (currentContext, page); // draws the page in the graphics context
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString* imagePath = [localDocuments stringByAppendingPathComponent: #"Karte.png"];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];
But this convert only the first page. Have any one an Idea how I can convert all pages into one png?
Related
I need to take a screen shot of a screen and save as pdf. I have accomplished the save as pdf task however the screenshot i take always gives a blank pdf. I have no idea why. My code is as follows :
-(IBAction)takeScreenShot
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *newImage = [[UIImageView alloc] initWithImage:image];
UIGraphicsEndImageContext();
[self createPDFfromUIView:newImage saveToDocumentsWithFileName:#"SecondScreen1.pdf"];
}
-(void)createPDFfromUIView:(UIImageView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
//CGSize pageSize = CGSizeMake(612, 792);
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
//[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
}
This line that's commented out should be writing your image to the pdf. Put that code back in and it might work.
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
If that's failing (with no errors) make sure that UIImage *image is not blank.
How might one convert a screenshot, taken on an iPhone for example, into a PDF file. It's easy enough to take the screenshot and put it into a UIImage, but it's the conversion which has me stumped. I took a look at the Quartz framework which is the only one in Xcode that I know to support the PDF format, but couldn't find anything there to make this work. Is there anything native to Xcode that I'm missing? If not, is there a public framework somewhere that could handle a conversion like this?
I figured it out. It involved saving a screenshot as a UIImage, and then using the very helpful tutorial found here to get me going with the PDF conversion. As it turns out, there are functions to handle the making of PDF documents in the Core Graphics class.
You wouldn't convert a screenshot to a pdf. You would create a screenshot, create a pdf and insert the screenshot image into the first page of the pdf.
Untested code to create image:
nameStr = [NSString stringWithFormat:#"myImage.png"];
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
namePath = [documentsDirectory stringByAppendingString:#"/"];
namePath = [thumbnailPath nameStr];
CGRect contextRect = CGRectMake(0, 0, 1024, 768);
UIGraphicsBeginImageContext(contextRect.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImagePNGRepresentation(viewImage);
UIImage *myImage = [[UIImage alloc] initWithData:data];
Untested code to add image to a pdf:
pageSize = CGSizeMake(1024, 768);
fileName = #"myFile.pdf";
pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
[myImage drawInRect:CGRectMake( 0, 0, 1024, 768];
UIGraphicsEndPDFContext();
p.s. assuming ipad 1024 x 768 above
There seems to be a scatter of info on this topic, but it seems rather a muddle, so I hoe you don't mind me asking.
I have a UIImagePickerController working, letting me either take a photo or pick on from the library and then set a UIImageView of my choice to show that image taken/selected.
However, how can I firstly save the image, then load it back in when the application is reopened? Also, I will be saving multiple images so they need to be uniquely identified.
Any help will be massively appreciated, thanks.
I don't know of a way to get the Photo from the Photo Album a Second time. There does not seem to be a way to retrieve it again. We do a few things to save the Image internally.
We either convert the Image to NSData and save it in CoreData, or we save it off to our sandbox.
here is a snip of code that we use, this is doing one of a few things, I'm getting the Image from a ScreenShot, though its the same once you have the UIImage.
// go get our screenshot
UIImage* screenShot = [self createScreenShotThumbnailWithWidth:200];
// Save screen shot as a png in the documents directory with the UUID of the IoCDScreen
// Saving to Sandbox
[self saveImage:screenShot withName:currentScreen.itemUUID];
// save image to Photo Album - though you can't get a ref back to it
UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil);
//convert our screen shot PNG to NSData and store it in a CoreData Managed Object
currentScreen.screenImageDevelopment = [NSData dataWithData: UIImagePNGRepresentation( screenShot )];
Helper Functions used above
//--------------------------------------------------------------------------------------------------------
// saveImage:withName:
// Description: Save the Image with the name to the Documents folder
//
//--------------------------------------------------------------------------------------------------------
- (void)saveImage:(UIImage *)image withName:(NSString *)name {
//grab the data from our image
NSData *data = UIImageJPEGRepresentation(image, 1.0);
//get a path to the documents Directory
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add out name to the end of the path with .PNG
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", name]];
//Save the file, over write existing if exists.
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}
//--------------------------------------------------------------------------------------------------------
// createScreenShotThumbnailWithWidth
// Description: Grab a screen shot and then scale it to the width supplied
//
//--------------------------------------------------------------------------------------------------------
-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{
// Size of our View
CGSize size = self.view.bounds.size;
//First Grab our Screen Shot at Full Resolution
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Calculate the scal ratio of the image with the width supplied.
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = width / size.width;
} else {
ratio = width / size.height;
}
//Setup our rect to draw the Screen shot into
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
//Scale the screenshot and save it back into itself
UIGraphicsBeginImageContext(rect.size);
[screenShot drawInRect:rect];
screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Send back our screen shot
return screenShot;
}
i am creating photo frame application how can i merge photo and frame in one single png and save it to the bundle or document folder.
//UIGraphicsBeginImageContext(im1.frame.size);
UIGraphicsBeginImageContext(im2.frame.size);
[im1.image drawInRect:CGRectMake(0.0, 0.0, im2.frame.size.width, im2.frame.size.height)];
[backdrop.layer renderInContext:UIGraphicsGetCurrentContext()];
[im2.image drawInRect:CGRectMake(0.0, 0.0, im2.frame.size.width, im2.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//NSData *dataObj = UIImagePNGRepresentation(image);
//NSString *picturedata = [dataObj base64Encoding];
appDelegate.fbpost=image;
//appDelegate.image11=image;
[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
// Set the background
//SETIMAGE(image);
// Show the current contents of the documents folder
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:#"/Documents"]]);
So I know you can get a CGImage from a file using UIImage...
UIImage *img = [UIImage imageNamed:#"name.bmp"];
[img CGImage];
But, is there a way to get a CGImageRef without using a UIImage?
(I am trying this in a static library which doesn't have access to UIKit) I could use other frameworks if necessary, just not UIKit. Would CIImage work? Or NSBitmapImageRep?
You can get it from NSData:
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:#""]);
CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); // Or JPEGDataProvider
You would have to be certain that the data was a JPEG or a PNG for this to work.
You have to understand that using [UIImage imageNamed:#"name.bmp"] works through UIKit framework. This is not thread safe. Example for dispatch_apply you must use Quartz framework.
// Through Quartz
NSString *maskFilePath = [[NSBundle mainBundle] pathForResource:#"mask" ofType:#"png"];
CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([maskFilePath UTF8String]);
CGImageRef maskRef = CGImageCreateWithPNGDataProvider(dataProvider, NULL, true, kCGRenderingIntentDefault);