【问题标题】:Vectorial PDF graphic elements on iOSiOS 上的矢量 PDF 图形元素
【发布时间】:2011-04-01 18:52:32
【问题描述】:

目前在 iOS 上实现矢量界面元素非常奇特,UIImage 宣传仅支持光栅格式,但我可以在 IB 中将 pdf 文件设置为 UIButton 的图像,并且它具有良好的抗锯齿效果,但是图像在运行 iOS 4.x 和 3.x 的 iphone 或 ipad 上不可见,让它显示的唯一方法是在代码中重新创建相同的按钮并省略 .pdf 扩展名:

searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
[self.view addSubview:searchButton]; 

但是,这仅显示了 iOS 4.x 上的图像,并且具有相当大的调整大小像素化并且没有抗锯齿,如图所示:

除了为什么它看起来这么糟糕、为什么它只在 4.x 中有效以及为什么 IB 版本根本不能工作这些显而易见的问题之外,有没有人知道在应用程序中正确使用矢量艺术的任何方法?

它不一定是 PDF,但我看到 Apple 在 mac 端应用程序上使用了很多,上面的代码和 IB 方法都可以在 OSX 应用程序上完美运行顺便说一句。

【问题讨论】:

    标签: iphone cocoa-touch pdf ipad ios


    【解决方案1】:

    由于 iOS 错过了 PDFKit.framework,我猜想 UIImage 有限/损坏的 PDF 支持的问题在于它不应该首先获得任何支持,在这方面我报告了它的有限支持作为一个错误(rdar:8338627),它也是 IB 中的一个错误,渲染支持可能是从 osx 继承的。

    我已经决定在上下文中手动渲染 pdf,然后将其保存到 UIImage,其代码如下(在 iOS 3.x 和 4.x 上测试)

    #include <dlfcn.h>
    
    -(UIImage *)UIImageFromPDF:(NSString*)fileName size:(CGSize)size{
        CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)fileName, NULL, NULL);  
        if (pdfURL) {       
            CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);
            CFRelease(pdfURL);  
            //create context with scaling 0.0 as to get the main screen's if iOS4+
            if (dlsym(RTLD_DEFAULT,"UIGraphicsBeginImageContextWithOptions") == NULL) {
                UIGraphicsBeginImageContext(size);      
            }else {
                UIGraphicsBeginImageContextWithOptions(size,NO,0.0);        
            }
            CGContextRef context = UIGraphicsGetCurrentContext();       
            //translate the content
            CGContextTranslateCTM(context, 0.0, size.height);   
            CGContextScaleCTM(context, 1.0, -1.0);      
            CGContextSaveGState(context);   
            //scale to our desired size
            CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
            CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, size.width, size.height), 0, true);
            CGContextConcatCTM(context, pdfTransform);
            CGContextDrawPDFPage(context, page);    
            CGContextRestoreGState(context);
            //return autoreleased UIImage
            UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();     
            UIGraphicsEndImageContext();
            CGPDFDocumentRelease(pdf);      
            return ret;     
        }else {
            NSLog(@"Could not load %@",fileName);
        }
        return nil; 
    }
    

    【讨论】:

      【解决方案2】:

      有一个很棒的项目可以帮助将 PDF 渲染成 UIImages。

      https://github.com/mindbrix/UIImage-PDF

      【讨论】:

        【解决方案3】:

        我不知道为什么 UIImage 不能很好地处理 PDF,但是应该可以编写自己的代码来绘制 PDF。请参阅 Quartz 2D 编程指南中的“打开和查看 PDF”部分。您可以将 PDF 绘制到 CGBitmapContext 中,使用它来创建 CGImage,然后使用它来创建 UIImage。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-02
          • 2018-11-08
          相关资源
          最近更新 更多