【问题标题】:Displaying the pdf generated thumb nail in UIImage View Clearly在 UIImage 视图中清晰地显示 pdf 生成的缩略图
【发布时间】:2014-01-05 02:48:37
【问题描述】:

我已经为 pdf 文件创建了缩略图。所以每当用户单击任何一个缩略图时,它都应该全屏显示。所以我使用 UIImageView 来显示它。问题是。而不是显示

但它的显示是这样的,不清楚....

我用来显示的代码是

(void) displayPdf
{

CGFloat width = 60.0;

    NSLog(@"%d",i);

        //UIImage* thumbnailImage;

    NSLog(@"%@",fileName);

//        NSString*fileName= @"2_slo_sample";

        NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];

        NSURL* pdfFileUrl = [NSURL fileURLWithPath:path];

        CGPDFPageRef page;

    CGRect pageRect = CGRectMake(0, 0, 150,150);

    CGFloat pdfScale = width/pageRect.size.width;

    pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);

    pageRect.origin = CGPointZero;

        UIGraphicsBeginImageContext(pageRect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfFileUrl);

        //NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);


           CGContextSaveGState(context);

    CGContextTranslateCTM(context, 0.0, pageRect.size.height);

    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetGrayFillColor(context, 1.0, 1.0);

    CGContextFillRect(context, pageRect);

    // Grab the first PDF page
    page = CGPDFDocumentGetPage(pdf, i);

    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, false);
    // And apply the transform.
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true));

    CGContextDrawPDFPage(context, page);

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

    UIGraphicsEndImageContext();    
    CGPDFDocumentRelease(pdf);

    //webView.hidden = YES;
    imageView1 = [[UIImageView alloc] init];
    imageView1.frame =CGRectMake(0, 50, 280, 280);
    [imageView1 setContentMode:UIViewContentModeScaleAspectFit];
    [imageView1 setNeedsDisplay];
    imageView1.backgroundColor = [UIColor clearColor];
    [imageView1 setUserInteractionEnabled:YES];
        imageView1.image = thumbnailImage;
    [self.view addSubview:imageView1];
}

请帮助我,以便我可以清楚地显示它。

提前致谢

【问题讨论】:

    标签: ios iphone objective-c uiimage pdf-generation


    【解决方案1】:

    您正在从 pdf 页面创建图像视图。这一切都很好而且很花哨,但我认为您正在以小缩略图大小创建此图像,然后,当您需要时,您将该图像调整为全屏。这就像常见的 CSI 错误想法(增强、增强、增强……)。由于您创建了大小有限的图像视图,因此您不能仅仅增加它的大小并期望自动显示像素清晰度。

    你有几个不同的选择:

    您可以将缩略图设为整个页面大小,然后将其缩小为缩略图大小(这将占用大量内存,可能会占用太多内存,具体取决于您的 pdf 文件的大小)。

    您可以在图像大小发生变化时重新创建图像视图的图像(这会占用大量处理能力,并且可能会使 UI 陷入困境,从而引起不适)。

    或者您可以在自定义 UIView 的 drawRect: 函数中绘制 pdf。这是最常见的(根据我的阅读)。然后,当您更改 UIView 的大小以使其全屏显示时,您只需将其告知setNeedsDisplay。对于此选项的一个非常好的代码示例,我建议您查看PDFKitten。它可能比您所寻找的要深入得多,但它对我有很大帮助。

    【讨论】:

    • 是的,谢谢分配伙计,我已经理解我所犯的错误......谢谢你的帮助......
    • 好 j.Chandrasekhar Reddy。一个人只能从错误中学习。我很感激
    猜你喜欢
    • 2019-11-23
    • 2011-02-21
    • 2016-12-14
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多