2014-09-27 67 views
0

我可以創建PDF並將JPG圖像添加到它。問題是我只能以非常低的分辨率獲取圖像的一小部分。我如何將iPad相機拍攝的照片放入PDF中?任何幫助將不勝感激。如何從使用IPAD相機拍攝的jpg圖像創建PDF頁面?

#import "MTViewController.h" 
#define kPadding 20 

@interface MTViewController() 
{ 
    CGSize _pageSize; 
} 
@end 

@implementation MTViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (IBAction)didClickOpenPDF 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"NextPDF.pdf"]; 

    if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) 
    { 
     ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfPath password:nil]; 

     if (document != nil) 
     { 
      ReaderViewController *readerViewController = [[ReaderViewController alloc]         initWithReaderDocument:document]; 
      readerViewController.delegate = self; 
      readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
      readerViewController.modalPresentationStyle = UIModalPresentationFullScreen; 
      [self presentModalViewController:readerViewController animated:YES]; 
     } 
    } 
} 

- (IBAction)didClickMakePDF 
{ 
    [self setupPDFDocumentNamed:@"NextPDF" Width:1024 Height:768]; 
    [self beginPDFPage]; 
    UIImage *anImage = [UIImage imageNamed:@"Test_1.JPG"]; 
    CGRect imageRect = [self addImage:anImage 
        atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), + kPadding)]; 
    [self beginPDFPage]; 
    [self finishPDF]; 
} 
- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height 
{ 
    _pageSize = CGSizeMake(width, height); 
    NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", name]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName]; 
    UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); 
} 

- (void)beginPDFPage 
{ 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, _pageSize.width, _pageSize.height), nil); 
} 

- (void)finishPDF 
{ 
    UIGraphicsEndPDFContext(); 
} 

- (CGRect)addImage:(UIImage*)image atPoint:(CGPoint)point 
{ 
    CGRect imageFrame = CGRectMake(point.x, point.y, image.size.width, image.size.height); 
    [image drawInRect:imageFrame]; 
    return imageFrame; 
} 

- (void)dismissReaderViewController:(ReaderViewController *)viewController 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 
@end 

回答

0

您在addImage:atPoint:方法中創建的框架都是錯誤的。您需要創建一個實際適合頁面的框架。

您的頁面是1024x768(這是PDF的奇怪大小)。 PDF的大小應該是72點(每英寸點數)。

然後指定圖像的原點應該是頁面的中心。然後,您將圖像的框架設置爲圖像的大小。完整的圖像將比PDF頁面大得多。

如果您想讓圖片填充頁面,請將圖片的框架設置爲0, 0, 1024, 768(根據需要調整您可能需要的任何邊距)。然後將完整大小的圖像繪製到該框架中。圖像將被縮放以適合。您可能還需要調整邊框以使其與原始圖像具有相同的縱橫比,以避免圖像失真。

相關問題