Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2009, 04:50 AM   #1 (permalink)
iF9
Registered Member
 
Join Date: Nov 2009
Posts: 1
Default 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!
iF9 is offline   Reply With Quote
Old 12-04-2009, 04:35 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 1
Default

Quote:
Originally Posted by iF9 View Post
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!!!
tmd4 is offline   Reply With Quote
Reply

Bookmarks

Tags
cgimageref, cgpdfpage, coregraphics, pdf, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:01 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0