簡體   English   中英

iOS上的矢量PDF圖形元素

[英]Vectorial PDF graphic elements on iOS

目前在iOS上實現矢量界面元素是非常奇特的,UIImage宣傳僅支持柵格格式,但我能夠將pdf文件設置為IB中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應用程序BTW上完美地工作。

由於iOS錯過了PDFKit.framework,我猜UIImage受限制/破壞PDF支持的問題是它首先不應該有任何支持,在這方面我報告了它作為一個bug有限的支持(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; 
}

有一個很棒的項目可以幫助將PDF渲染成UIImages。

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

我不知道為什么UIImage與PDF不兼容,但應該可以編寫自己的代碼來繪制PDF。 請參閱“Quartz 2D編程指南”中的“打開和查看PDF”部分。 您可以將PDF繪制到CGBitmapContext中,使用它來創建CGImage,並使用它來創建UIImage。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM