2015-12-30 14 views
7

में पीडीएफ प्रस्तुत करना मैं अपने टीवीओएस ऐप के अतिरिक्त काम कर रहा हूं जो ऐप में संग्रहीत पीडीएफ देखने की अनुमति देगा। हालांकि, UIWebView के बिना, मुझे यह कैसे करना है इस पर एक नुकसान है। मैंने अन्य स्थानों पर प्रश्न पूछे हैं, और एपीआई के बारे में ऐप्पल से एक शब्दकोष और असहाय दस्तावेज़ के लिंक के साथ बधाई दी जिसका उपयोग किया जा सकता है, और यहां तक ​​कि यहां इसका संदर्भ दिया गया है (सीजीपीडीएफपीज) लेकिन इसे लागू करने के तरीके पर कोई वास्तविक मार्गदर्शिका नहीं है । क्या किसी ने टीवीओएस पर सफलतापूर्वक ऐसा किया है, और यदि हां, तो क्या आप इस प्रक्रिया में शुरू करने में मेरी मदद करेंगे?ऐप्पल टीवी टीवीओएस

+0

आपको जो लिंक भेजा गया है वह क्या है? CGPDFPage होने से सामान्य रूप से आपकी सहायता नहीं होगी क्योंकि इसमें पृष्ठ के बारे में केवल "मेटाडाटा" जानकारी है लेकिन स्वयं ही इसकी सामग्री प्रस्तुत नहीं कर सकती है। यह आमतौर पर CGContext है जिसमें इस तरह का एक पृष्ठ लेने और इसे प्रस्तुत करने की क्षमता है; लेकिन यह बहुत अच्छा हो सकता है कि एपीआई उपलब्ध नहीं है (यहां देखें कि यह है: http://stackoverflow.com/questions/4639781/rendering-a-cgpdfpage-into-a-uiimage) –

उत्तर

5

नीचे कुछ कोड है कि मैं ने लिखा है और tvOS में परीक्षण किया गया है। ध्यान दें कि यह उद्देश्य-सी में है।

मैंने नौकरी करने के लिए दो कार्य किए हैं, और एक सहायक कार्य को यूआईएसक्रॉल व्यू में पीडीएफ छवियों को प्रदर्शित करने के लिए बनाया है। पहला यूआरएल से पीडीएफ दस्तावेज खोल देगा। एक वेब यूआरएल इस्तेमाल किया गया था। इस नमूने में एक स्थानीय फाइल भी इस्तेमाल की जा सकती है।

स्थानीय फ़ाइल से दस्तावेज़ खोलने के लिए एक सहायक कार्य भी है।

दूसरा फ़ंक्शन किसी संदर्भ में पीडीएफ दस्तावेज प्रस्तुत करता है। मैंने इसे एक छवि बनाकर संदर्भ प्रदर्शित करना चुना। संदर्भ को संभालने के अन्य तरीके भी हैं।

दस्तावेज़ खोलना काफी सीधे आगे है, इसलिए इसके लिए कोड में कोई टिप्पणी नहीं है। दस्तावेज़ को प्रस्तुत करना थोड़ा अधिक शामिल है, और इसलिए उस कार्य को समझाते हुए टिप्पणियां हैं।

पूरा आवेदन नीचे है।

- (CGPDFDocumentRef)openPDFLocal:(NSString *)pdfURL { 
    NSURL* NSUrl = [NSURL fileURLWithPath:pdfURL]; 

    return [self openPDF:NSUrl]; 
} 

- (CGPDFDocumentRef)openPDFURL:(NSString *)pdfURL { 
    NSURL* NSUrl= [NSURL URLWithString:pdfURL]; 

    return [self openPDF:NSUrl]; 
} 

- (CGPDFDocumentRef)openPDF:(NSURL*)NSUrl { 
    CFURLRef url = (CFURLRef)CFBridgingRetain(NSUrl); 

    CGPDFDocumentRef myDocument; 
    myDocument = CGPDFDocumentCreateWithURL(url); 
    if (myDocument == NULL) { 
     NSLog(@"can't open %@", NSUrl); 
     CFRelease (url); 
     return nil; 
    } 
    CFRelease (url); 

    if (CGPDFDocumentGetNumberOfPages(myDocument) == 0) { 
     CGPDFDocumentRelease(myDocument); 
     return nil; 
    } 

    return myDocument; 
} 
- (void)drawDocument:(CGPDFDocumentRef)pdfDocument 
{ 
    // Get the total number of pages for the whole PDF document 
    int totalPages= (int)CGPDFDocumentGetNumberOfPages(pdfDocument); 
    NSMutableArray *pageImages = [[NSMutableArray alloc] init]; 

    // Iterate through the pages and add each page image to an array 
    for (int i = 1; i <= totalPages; i++) { 
     // Get the first page of the PDF document 
     CGPDFPageRef page = CGPDFDocumentGetPage(pdfDocument, i); 
     CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 

     // Begin the image context with the page size 
     // Also get the grapgics context that we will draw to 
     UIGraphicsBeginImageContext(pageRect.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 

     // Rotate the page, so it displays correctly 
     CGContextTranslateCTM(context, 0.0, pageRect.size.height); 
     CGContextScaleCTM(context, 1.0, -1.0); 
     CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true)); 

     // Draw to the graphics context 
     CGContextDrawPDFPage(context, page); 

     // Get an image of the graphics context 
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     [pageImages addObject:image]; 
    } 
    // Set the image of the PDF to the current view 
    [self addImagesToScrollView:pageImages]; 
} 

-(void)addImagesToScrollView:(NSMutableArray*)imageArray { 
    int heigth = 0; 
    for (UIImage *image in imageArray) { 
     UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
     imgView.frame=CGRectMake(0, heigth, imgView.frame.size.width, imgView.frame.size.height); 
     [_scrollView addSubview:imgView]; 
     heigth += imgView.frame.size.height; 
    } 
} 

और यह सब एक साथ टाई, तो आप ऐसा कर सकते हैं:

CGPDFDocumentRef pdfDocument = [self openPDFURL:@"http://www.guardiansuk.com/uploads/accreditation/10testing.pdf"]; 
[self drawDocument:pdfDocument]; 

ध्यान दें कि मैं एक यादृच्छिक पीडीएफ कि वेब पर मुक्त करने के लिए उपलब्ध था का उपयोग कर रहा हूँ। मैं https यूआरएल के साथ कुछ समस्याओं में भाग गया, लेकिन मुझे यकीन है कि इसे हल किया जा सकता है, और यह वास्तव में पीडीएफ उद्घाटन प्रश्न से संबंधित नहीं है।

+0

धन्यवाद। जब मैं वापस आऊंगा तो मैं इसे एक बार जाऊंगा। अधिकांश पीडीएफ ऐप के भीतर संग्रहीत किए जाने चाहिए (मुझे लगता है कि मैं इसे प्राप्त कर सकता हूं और अभी भी 200 एमबी सीमा से कम हो सकता है) – user717452

+0

त्वरित प्रश्न ... मेरे पास अब ऐप में वीडियो सुविधाओं और टीवीएमएल ऐप के उपयोग के साथ स्टोर में ऐप उपलब्ध है। इसे इस तरह चलाने के लिए, क्या इसे एक कस्टम ऐप होना चाहिए? – user717452

+0

दुर्भाग्य से, यह वास्तव में काम नहीं करेगा, क्योंकि इस परियोजना में शामिल लगभग हर पीडीएफ में कई पेज हैं। – user717452

0

टीवीओएस दस्तावेज़ में creating, viewing and transforming PDF documents पर एक अनुभाग शामिल है, इसलिए मुझे लगता है कि इसमें आपकी आवश्यक कार्यक्षमता शामिल है।

उस पृष्ठ पर बहुत सारे उदाहरण कोड हैं, लेकिन यहां कुछ कोड है जो मैं उसी उद्देश्य के लिए आईओएस पर उपयोग करता हूं। यह tvOS पर काम करना चाहिए, लेकिन मैं इसे परीक्षण करने के लिए एक तरीका नहीं है:

func imageForPDF(URL: NSURL, pageNumber: Int, imageWidth: CGFloat) -> UIImage { 
    let document = CGPDFDocumentCreateWithURL(URL) 
    let page = CGPDFDocumentGetPage(document, pageNumber) 
    var pageRect = CGPDFPageGetBoxRect(page, .MediaBox) 
    let scale = imageWidth/pageRect.size.width 
    pageRect.size = CGSizeMake(pageRect.size.width * scale, pageRect.size.height * scale) 
    pageRect.origin = CGPointZero 

    UIGraphicsBeginImageContext(pageRect.size) 
    let ctx = UIGraphicsGetCurrentContext() 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0) // White background 
    CGContextFillRect(ctx, pageRect) 
    CGContextSaveGState(ctx) 

    // Rotate the PDF so that it’s the right way around 
    CGContextTranslateCTM(ctx, 0.0, pageRect.size.height) 
    CGContextScaleCTM(ctx, 1.0, -1.0) 
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, .MediaBox, pageRect, 0, true)) 

    CGContextDrawPDFPage(ctx, page) 
    CGContextRestoreGState(ctx) 

    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return image 
} 
संबंधित मुद्दे