我可以创建PDF并将jpg图像添加到其中。问题是我只能以非常低的分辨率获得图像的一小部分。如何将iPad相机拍摄的照片放入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 :(得分:0)
您在addImage:atPoint:
方法中创建的框架完全错误。您需要创建一个实际适合页面的框架。
您的页面是1024x768(对于PDF来说这是一个奇怪的大小)。 PDF的大小应为密度为72点(每英寸点数)的点。
然后指定图像的原点应该是页面的中心。然后将图像的帧设置为图像的大小。完整图像将比PDF页面大小大得多。
如果您希望图片填满页面,请将图片的框架设置为0, 0, 1024, 768
(根据需要调整您可能需要的任何边距)。然后将完整大小的图像绘制到该帧中。图像将缩放以适合。您可能还需要调整框架以使其具有与原始图像相同的纵横比,以避免图像失真。