iOS下PDF文件的浏览和涂鸦效果的简单实现
浏览pdf的效果
方法一:利用webview
-(void)loaddocument:(nsstring *)documentname inview:(uiwebview *)webview
{
nsstring *path = [[nsbundle mainbundle] pathforresource:documentname oftype:nil];
nsurl *url = [nsurl fileurlwithpath:path];
nsurlrequest *request = [nsurlrequest requestwithurl:url];
[webview loadrequest:request];
}
利:1.实现简单
2.还是实现简单
弊:1.仅能浏览,拿不到任何回调,safari不会鸟任何人。
2.固定竖版拖动,想实现翻页动效果就扒瞎
下面的方法可以解决webview 显示pdf的弊,相对的,要付出一些汗水作为代价了。
方法二:利用cgcontextdrawpdfpage
cgpdfdocumentref getpdfdocumentref(nsstring *filename)
{
cfstringref path;
cfurlref url;
cgpdfdocumentref document;
size_t count;
path = cfstringcreatewithcstring (null, [filename utf8string], kcfstringencodingutf8);
url = cfurlcreatewithfilesystempath (null, path, kcfurlposixpathstyle, 0);
cfrelease (path);
document = cgpdfdocumentcreatewithurl (url);
cfrelease(url);
count = cgpdfdocumentgetnumberofpages (document);
if (count == 0) {
printf("[%s] needs at least one page!\n", [filename utf8string]);
return null;
} else {
printf("[%ld] pages loaded in this pdf!\n", count);
}
return document;
}
void displaypdfpage (cgcontextref mycontext, size_t pagenumber, nsstring *filename)
{
cgpdfdocumentref document;
cgpdfpageref page;
document = getpdfdocumentref (filename);
page = cgpdfdocumentgetpage (document, pagenumber);
cgcontextdrawpdfpage (mycontext, page);
cgpdfdocumentrelease (document);
}
这样显示出来的pdf单页是倒立的,quartz坐标系和uiview坐标系不一样所致,调整坐标系,使pdf正立:
cgcontextref context = uigraphicsgetcurrentcontext();
cgcontexttranslatectm(context, 80, self.frame.size.height-60);
cgcontextscalectm(context, 1, -1);
配合ios5强大的uipageviewcontroller实现翻页浏览
- (pdfviewcontroller *)viewcontrolleratindex:(nsuinteger)index
{
//return the pdfviewcontroller for the given index.
if (([self.pagepdf count] == 0 )|| (index > [self.pagepdf count]) ) {
return nil;
}
//create a new view controller and pass suitable data.
pdfviewcontroller *dataviewcontroller = [[pdfviewcontroller alloc]initwithnibname:@"pdfviewcontroller" bundle:nil];
//dataviewcontroller.pdfview = [self.pagepdf objectatindex:index];
dataviewcontroller.pdfview = [[pdfview alloc]initwithframe:self.view.frame atpage:index];
[dataviewcontroller.view addsubview:dataviewcontroller.pdfview];
nslog(@"index = %d",index);
return dataviewcontroller;
}
- (nsuinteger) indexofviewcontroller:(pdfviewcontroller *)viewcontroller
{
return [self.pagepdf indexofobject:viewcontroller.pdfview];
}
- (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerbeforeviewcontroller:(uiviewcontroller *)viewcontroller
{
nsuinteger index = [self indexofviewcontroller:(pdfviewcontroller *)viewcontroller];
if ((index == 0 ) || (index == nsnotfound)){
return nil;
}
index--;
return [self viewcontrolleratindex:index];
}
- (uiviewcontroller *)pageviewcontroller:(uipageviewcontroller *)pageviewcontroller viewcontrollerafterviewcontroller:(uiviewcontroller *)viewcontroller
{
nsuinteger index = [self indexofviewcontroller:(pdfviewcontroller *)viewcontroller];
if (index == nsnotfound)
{
return nil;
}
index++;
if (index == [self.pagepdf count]){
return nil;
}
return [self viewcontrolleratindex:index];
}
涂鸦效果
主要涉及:
1. 多context,分层画画
- (void)drawlayer:(calayer *)layer incontext:(cgcontextref)ctx
2. 触摸事件touches族那些event
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event
……
3. 初始化单页view传页码
- (id)initwithframe:(cgrect)frame onpage:(nsinteger)page
4.画轨迹方法
cg_extern void cgpathmovetopoint(cgmutablepathref path,
const cgaffinetransform *m, cgfloat x, cgfloat y)
cg_extern void cgpathaddlinetopoint(cgmutablepathref path,
const cgaffinetransform *m, cgfloat x, cgfloat y)