2014-09-27 72 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(根据需要调整您可能需要的任何边距)。然后将完整大小的图像绘制到该框架中。图像将被缩放以适合。您可能还需要调整边框以使其与原始图像具有相同的纵横比,以避免图像失真。