@interface MMPDFView : UIView {
CGPDFDocumentRef pdfDoc;
}
- (void)loadPDF:(NSURL *)url;
- (void)drawPdfPage:(int)page context:(CGContextRef)ctxt area:(CGRect)vrect isZoom:(BOOL)isZoom;
@end
@implementation MMPDFView
- (void)loadPDF:(NSURL *)url
{
if(pdfDoc != nil)
CGPDFDocumentRelease(pdfDoc);
pdfDoc = CGPDFDocumentCreateWithURL((CFURLRef)url);
[self setNeedsDisplay];
}
- (void)drawPdfPage:(int)page context:(CGContextRef)ctxt area:(CGRect)vrect
{
CGContextSetFillColorWithColor(ctxt, [UIColor whiteColor].CGColor);
CGContextFillRect(ctxt, self.frame);
int maxPage = CGPDFDocumentGetNumberOfPages(pdfDoc);
if(page > 0 && currentPage <= maxPage)
{
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, page);
CGRect pageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
double hRate = vrect.size.width / pageRect.size.width;
double xstd = 0, ystd = pageRect.size.height * hRate;
if(pageRect.size.height * hRate <= vrect.size.height)
ystd = pageRect.size.height * hRate + (vrect.size.height - pageRect.size.height * hRate) / 2;
if(pageRect.size.width * hRate < vrect.size.width)
xstd = (vrect.size.width - pageRect.size.width * hRate) / 2;
CGContextTranslateCTM(ctxt, xstd, ystd);
CGContextScaleCTM(ctxt, hRate, -hRate);
CGContextSaveGState(ctxt);
CGContextDrawPDFPage(ctxt, pdfPage);
CGContextRestoreGState(ctxt);
}
}
- (void)drawRect:(CGRect)rect
{
if(pdfDoc == nil)
return;
CGContextRef ctxt = UIGraphicsGetCurrentContext();
[self drawPdfPage:0 context:ctxt area:self.frame];
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self loadPDF:파일경로];
}
return self;
}
- (void)dealloc {
if(pdfDoc != nil)
CGPDFDocumentRelease(pdfDoc);
[super dealloc];
}
@end
'iOS Programming' 카테고리의 다른 글
애니메이션효과로 탭바 숨기고 보이기 (0) | 2010.09.10 |
---|---|
UINavigationBar에 이미지를 배경으로 지정하기 (0) | 2010.09.10 |
UITableViewCell에 Badge 달기 (0) | 2010.09.10 |
iPhone SDK 4.0에서 기존 프로젝트의 Base SDK missing 문제 해결하기 (1) | 2010.06.22 |
TabBar와 NavigationBar의 조합 (1) | 2010.05.08 |