Statt Laden pdf in webView würde ich Ihnen PDF in UIView ziehen vorschlagen mögen, wie ich in meinem eigenen Projekt getan haben und es funktioniert perfekt. Unten ist meine UIView SubClass, die PDF im UIView Kontext zeichnet.
PDFPage.h
#import <UIKit/UIKit.h>
@protocol PDFPageDelegate;
@interface PDFPage : UIView
@property uint currentPage;
@property unsigned long totalPages;
@property CGRect pageRect;
@property NSString *pdfPath;
@property id<PDFPageDelegate> delegate;
- (CGRect)setPdf:(NSString*)filePath;
- (void)swipeLeft;
- (void)swipeRight;
- (void)setPageNumber:(NSUInteger)targetPageNumber;
@end
@protocol PDFPageDelegate <NSObject>
- (void)pdfWillSwipeToLeft:(uint)upcommingPageNumber;
- (void)pdfWillSwipeToRight:(uint)upcommingPageNumber;
- (void)pdfTaped:(CGPoint)tapAt;
- (void)pdfDoubleTapped;
@end
PDFPage.m
#import "PDFPage.h"
@implementation PDFPage
@synthesize pageRect = _pageRect;
@synthesize currentPage = _currentPage;
@synthesize totalPages = _totalPages;
@synthesize delegate = _delegate;
@synthesize pdfPath = _pdfPath;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {}
_currentPage = 1;
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeRight];
return self;
}
- (void)setPageNumber:(NSUInteger)targetPageNumber
{
_currentPage = (uint)targetPageNumber;
[self setNeedsDisplay];
[UIView transitionWithView:self duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.layer displayIfNeeded];
} completion:nil];
}
- (void)swipeLeft
{
if(_currentPage == _totalPages)
return;
_currentPage++;
[_delegate pdfWillSwipeToLeft:_currentPage];
[self setNeedsDisplay];
[UIView transitionWithView:self duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.layer displayIfNeeded];
} completion:nil];
}
- (void)swipeRight
{
if(_currentPage == 1)
return;
_currentPage--;
[_delegate pdfWillSwipeToRight:_currentPage];
[self setNeedsDisplay];
[UIView transitionWithView:self duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.layer displayIfNeeded];
} completion:nil];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// PDF might be transparent, assume white paper
[[UIColor whiteColor] set];
CGContextFillRect(ctx, rect);
// Flip coordinates
CGContextGetCTM(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -rect.size.height);
// url is a file URL
CFURLRef pdfURL = (__bridge CFURLRef)[NSURL fileURLWithPath:_pdfPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);
CGPDFPageRef page1 = CGPDFDocumentGetPage(pdf, _currentPage);
// get the rectangle of the cropped inside
CGRect mediaRect = CGPDFPageGetBoxRect(page1, kCGPDFCropBox);
CGContextScaleCTM(ctx, rect.size.width/mediaRect.size.width,
rect.size.height/mediaRect.size.height);
CGContextTranslateCTM(ctx, -mediaRect.origin.x, -mediaRect.origin.y);
// draw it
CGContextDrawPDFPage(ctx, page1);
CGPDFDocumentRelease(pdf);
}
- (CGRect)setPdf:(NSString*)filePath
{
_pdfPath =filePath;
_currentPage = 1;
CFURLRef pdfURL = (__bridge CFURLRef)[NSURL fileURLWithPath:_pdfPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf,_currentPage);
_pageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
_totalPages = (CGPDFDocumentGetNumberOfPages(pdf));
[self setNeedsDisplay];
[UIView transitionWithView:self duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self setNeedsDisplay];
} completion:nil];
CGRect finalRect = CGRectMake(0, 0, _pageRect.size.width, _pageRect.size.height);
return finalRect;
}
Wie zu verwenden: -
NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfPath]]; // here pdfPath is webURL
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * savePDFAt = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
savePDFAt = [NSString stringWithFormat:@"%@/PDFs/",savePDFAt];
[[NSFileManager defaultManager] createDirectoryAtPath:savePDFAt withIntermediateDirectories:NO attributes:nil error:nil];
[savePDFAt stringByAppendingPathComponent:"test.pdf"];
if([pdfData writeToFile:savePDFAt options:0 error:&error])
NSLog(@"PDF download complete");
PDFPage *pdfPage = [PDFPage new];
pdfPage.alpha = 0.0f;
pdfPage.delegate = self;
pdfPage.frame = [pdfPage setPdf:pdfPath];
Dann fügen Sie diese PDFPAGE Selbst Sicht oder Auswahlrad.
Wenn Sie nur die Höhe des Dokuments benötigen, dann ist diese Frage ein Duplikat. Wenn Sie die Menge an belegtem Speicherplatz auf einer bestimmten Seite benötigen, sollten Sie Ihren Ansatz überdenken, da ein PDF-Format von Natur aus ein Portable Document Format ist, das über Plattformen und Browser hinausgeht. Wenn dieser Leerraum vom Ersteller des Dokuments beabsichtigt wurde, was wahrscheinlich der Fall ist, wenn PDF-Dokumente als Seiten erstellt werden, können Sie sie möglicherweise anzeigen. – SwiftArchitect