有沒有辦法將PDF加載到屏幕外的CALayer(或CATiledLayer)中,然後在需要時才顯示它?該解決方案還應支持所需分辨率的縮放。將PDF加載到屏幕外層
更新
只是爲了澄清 - 我想要的PDF仍然基於矢量。
有沒有辦法將PDF加載到屏幕外的CALayer(或CATiledLayer)中,然後在需要時才顯示它?該解決方案還應支持所需分辨率的縮放。將PDF加載到屏幕外層
更新
只是爲了澄清 - 我想要的PDF仍然基於矢量。
注意:您告訴您需要將PDF加載到CALayer中。但在這裏,我有解決方案將整個PDF頁面加載到圖像中。一旦圖像在數組中,您可以在需要時將其中任何一個視爲&。如果您有任何疑問,請添加評論。
首先把下面的方法放在你的視圖控制器的實現上面。
NSMutableArray *aRefImgs; // global variable.
void setRefImgs(NSMutableArray *ref){
aRefImgs=ref;
}
NSMutableArray* ImgArrRef(){
return aRefImgs;
}
CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename) {
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
document = CGPDFDocumentCreateWithURL (url);// 2
CFRelease(url);
int count = CGPDFDocumentGetNumberOfPages (document);// 3
if (count == 0) {
printf("`%s' needs at least one page!", filename);
return NULL;
}
return document;
}
void MyDisplayPDFPage (CGContextRef myContext,size_t pageNumber,const char *filename, CGPDFOperatorTableRef tblRef, NSMutableString *mainStr) {
CGPDFDocumentRef document;
CGPDFPageRef page;
document = MyGetPDFDocumentRef (filename);// 1
totalPages=CGPDFDocumentGetNumberOfPages(document);
page = CGPDFDocumentGetPage (document, pageNumber);// 2
CGContextDrawPDFPage (myContext, page);// 3
CGContextTranslateCTM(myContext, 0, 20);
CGContextScaleCTM(myContext, 1.0, -1.0);
CGPDFDocumentRelease (document);// 4
}
現在,將下面的代碼放在viewController.m文件中。
-(void)viewDidLoad {
filePath=[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"roadie" ofType:@"pdf"]];
CGPDFDocumentRef d=MyGetPDFDocumentRef([filePath UTF8String]);
size_t nos=CGPDFDocumentGetNumberOfPages(d);
totalPages=nos;
arrImgs=[[NSMutableArray alloc] init]; // arrImgs variable declared in viewController.h file.
setRefImgs(arrImgs);
int i;
for(i=0;i<nos;i++){
UIGraphicsBeginImageContext(CGSizeMake(720, 720));
MyDisplayPDFPage(UIGraphicsGetCurrentContext(), i+1, [filePath UTF8String],myTable,mainString);
UIImage *tmp=UIGraphicsGetImageFromCurrentImageContext();
UIImage *t2=[tmp rotate:UIImageOrientationDownMirrored];
[arrPDFImgs addObject:t2];
UIImageView *imgV=[[UIImageView alloc] initWithFrame:CGRectMake(scrPDFImages.frame.size.width * i, 0, scrPDFImages.frame.size.width, scrPDFImages.frame.size.height)];
[imgV setContentMode:UIViewContentModeScaleToFill];
[imgV setImage:t2];
[scrPDFImages addSubview:imgV];
// scrPDFImages is an array that has all images
// per page one image from pdf document
[imgV release];
UIGraphicsPopContext();
}
}
您可能需要圖像方向 - 我也已將整個課程放在這裏。
@interface UIImage (WBImage)
// rotate UIImage to any angle
-(UIImage*)rotate:(UIImageOrientation)orient;
@end
@implementation UIImage (WBImage)
static inline CGFloat degreesToRadians(CGFloat degrees)
{
return M_PI * (degrees/180.0);
}
static inline CGSize swapWidthAndHeight(CGSize size)
{
CGFloat swap = size.width;
size.width = size.height;
size.height = swap;
return size;
}
-(UIImage*)rotate:(UIImageOrientation)orient
{
CGRect bnds = CGRectZero;
UIImage* copy = nil;
CGContextRef ctxt = nil;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;
bnds.size = self.size;
rect.size = self.size;
switch (orient)
{
case UIImageOrientationUp:
return self;
case UIImageOrientationUpMirrored:
tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
break;
case UIImageOrientationDown:
tran = CGAffineTransformMakeTranslation(rect.size.width,
rect.size.height);
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;
case UIImageOrientationDownMirrored:
tran = CGAffineTransformMakeTranslation(0.0, rect.size.height);
tran = CGAffineTransformScale(tran, 1.0, -1.0);
break;
case UIImageOrientationLeft:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(0.0, rect.size.width);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;
case UIImageOrientationLeftMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height,
rect.size.width);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;
case UIImageOrientationRight:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, 0.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;
case UIImageOrientationRightMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeScale(-1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;
default:
// orientation value supplied is invalid
assert(false);
return nil;
}
UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();
switch (orient)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextScaleCTM(ctxt, -1.0, 1.0);
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0);
break;
default:
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
break;
}
CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(ctxt, rect, self.CGImage);
copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return copy;
}
@end
請將它添加到您的問題 - 你也想縮放。 – 2010-04-16 12:53:40