11-04-2009, 04:50 AM
#1 (permalink )
Registered Member
Join Date: Nov 2009
Posts: 1
Create a UIImage/CGImageRef from CGPDFPageRef
Hi guy..
I'm trying to do an application that show a page of a pdf file. I found a example to draw in UIScrollView and show the page but.. I wanna draw the page in a UIImage or CGImageRef.
I'm using this code, but it crash.
Code:
-(UIImage *)image {
UIGraphicsBeginImageContext(CGSizeMake(280, 320));
CGContextRef context = UIGraphicsGetCurrentContext();
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("/file.pdf"), NULL, NULL);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CGContextTranslateCTM(context, 0.0, 320);
CGContextScaleCTM(context, 1.0, -1.0);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 4);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
Can somebody help me? Thanks a lot!
12-04-2009, 04:35 PM
#2 (permalink )
Registered Member
Join Date: Dec 2009
Posts: 1
Quote:
Originally Posted by
iF9
Hi guy..
I'm trying to do an application that show a page of a pdf file. I found a example to draw in UIScrollView and show the page but.. I wanna draw the page in a UIImage or CGImageRef.
I'm using this code, but it crash.
Code:
-(UIImage *)image {
UIGraphicsBeginImageContext(CGSizeMake(280, 320));
CGContextRef context = UIGraphicsGetCurrentContext();
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("/file.pdf"), NULL, NULL);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CGContextTranslateCTM(context, 0.0, 320);
CGContextScaleCTM(context, 1.0, -1.0);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 4);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
Can somebody help me? Thanks a lot!
CFSTR("file.pdf") - its correctly! =) Thank you for you code it's helpfully for me! Thank you very match!!!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 273
20 members and 253 guests
ADY , Bertrand21 , Dani77 , HemiMG , iDifferent , IphoneSdk , jakerocheleau , JasonR , jimbo , macquitzon216 , MACralik , mer10 , NSeven , prchn4christ , Rudy , silverwiz , spiderguy84 , Sunny46
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp