2013-03-19 3 views
-1

UIGraphicsBeginPDFContextToFile 및 UIGraphicsBeginPDFPage를 사용하여 작성한 큰 hq PDF 파일에 축소판을 포함하고 텍스트 및 이미지를 그립니다.iOS는 PDF로 페이지 축소판을 임베드합니다.

페이지 엄지 손가락을 삽입하는 방법을 아는 사람이 있습니까?

챠오, 아르노

답변

0

당신은 PDF에서 썸네일을 작성하려는 경우, 당신은 아래의 코드를 사용할 수 있습니다. 이 코드는 디스크에 기록합니다. 메서드가 이미지를 반환하도록 쉽게 변경할 수 있습니다.

- (void)createPDFThumbnailForFile:(NSString *)theFilename { 
    if (!theFilename) {return;} 
    @try { 
     NSString *path = [FileInfo fullPathForFile:theFilename]; 
     NSURL *pdfFileUrl = [NSURL fileURLWithPath:path]; 
     CFURLRef pdfFileRef = (__bridge CFURLRef) pdfFileUrl; 
     CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfFileRef); 
     CGPDFPageRef page; 
     CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size 
     UIGraphicsBeginImageContext(aRect.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     UIImage *thumbnailImage; 
    // NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf); 
     //we only want the first page 
     for (int i = 0; i < 1; i++) { 
      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, 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(); 
      CGContextRestoreGState(context); 
     } 
     CGPDFDocumentRelease(pdf); 
     NSString *pngPath = [path stringByReplacingOccurrencesOfString:@".pdf" withString:@".png"]; 
    // [@"test" writeToFile:pngPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
     [UIImagePNGRepresentation(thumbnailImage) writeToFile:pngPath atomically:YES]; 
    } 
    @catch (NSException *exception) { 
     DebugLog(@"Could not write thumbnail to : %@ /n --> %@", theFileToSave, exception.description); 
    } 

} 
+0

죄송합니다. * 엄지 손가락을 그리는 방법을 알고 있지만 PDF의 페이지 축소판으로 포함해야합니다. – akw