iPad中的PDF缩略图很模糊

时间:2012-12-02 10:22:19

标签: iphone ipad pdf cgaffinetransform cgpdfdocument

我正在创建缩略图并在gridView中显示这些缩略图(使用AQgridView)。 gridView中显示的缩略图太模糊,不是我想要的。我附上了我的代码。请帮忙。

这是我的代码: -

- (void)viewDidLoad
{
[super viewDidLoad];
imageArray = [[NSMutableArray alloc] init];
self.gridView = [[AQGridView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.gridView.autoresizesSubviews = YES;
self.gridView.delegate = self;
self.gridView.dataSource = self;
[self.view addSubview:gridView];


[self makeThumbs];
// Do any additional setup after loading the view from its nib.
}

///////////用PDF文件制作拇指///////////////

-(void)makeThumbs{

NSString* finalPath = [[NSBundle mainBundle] pathForResource:@"Maths" ofType:@"pdf" inDirectory:nil];



NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfFileUrl);
CGPDFPageRef page;

CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();



NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);

for(int i = 0; i < totalNum; i++ ) {
     UIImage* thumbnailImage;

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, aRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetGrayFillColor(context, 1.0, 1.0);
    CGContextFillRect(context, aRect);


    // Grab the first PDF page
    page = CGPDFDocumentGetPage(pdf, i + 1);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, page);

    // Create the new UIImage from the context
    thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();

    //Use thumbnailImage (e.g. drawing, saving it to a file, etc)

    CGContextRestoreGState(context);
    [imageArray addObject:thumbnailImage];

}
NSLog(@"image array count is %d",[imageArray count]);
if ([imageArray count]>0) {
    [self.gridView reloadData];
}
UIGraphicsEndImageContext();    
CGPDFDocumentRelease(pdf);
}

此处的图像显示在iPad模拟器上创建的模糊缩略图enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用UIGraphicsBeginImageContextWithOptions传递0.0作为比例(使用屏幕比例)。