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