. Aşağıda, UIView bağlamında pdf çizen UIView alt sınıfı var.
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;
}
nasıl kullanılır: -
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];
Sonra kendiliğin görünümüne bu pdfPage eklemek veya Kaydıraç için.
İhtiyacınız olan tek şey belgenin yüksekliğiyse, bu soru bir kopyadır. Belirli bir sayfada yer alan alan miktarına ihtiyacınız varsa, yaklaşımınızı yeniden gözden geçirmek isteyebilirsiniz çünkü bir PDF, Doğaya Dayalı Belge Biçimi, platformları ve tarayıcıları aşmaktadır. Bu beyaz alan, belgenin yaratıcısı tarafından tasarlandıysa, büyük olasılıkla PDF belgelerinin sayfalar olarak oluşturulduğu zaman, bunu görüntülemeyi düşünebilirsiniz. – SwiftArchitect