2011-08-10 16 views
0

PDFのサムネイルを作成するために、私は自分のプロジェクトでこれらのコードセクションを試しました。もちろん、PDFの内容によっては、私の意見では修正可能なメモリ警告が出ます。Objective CでPDFのサムネイルを作成中にメモリ警告が発生する

私がCGContextConcatCTMメソッド呼び出しを取り除くと、それは問題ないと分かりました。しかし、私はこの方法が適切なサムネイルのために何をする必要があります。ここに私が試した方法です:

-(UIImage *)getThumbForPage:(int)page_number{ 
CGFloat width = 60.0; 

    // Get the page 
CGPDFPageRef page = [self getPage:page_number]; 

CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
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(); 

// White BG 
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
CGContextFillRect(context,pageRect); 

CGContextSaveGState(context); 

    // *********** 
// Next 3 lines makes the rotations so that the page look in the right direction 
    // *********** 
CGContextTranslateCTM(context, 0.0, pageRect.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true)); 

CGContextDrawPDFPage(context, page); 
CGContextRestoreGState(context); 

UIImage *thm = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
return thm; 

} 

、他方は次のとおりです。

NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath]; 
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl); 
CGPDFPageRef page; 

CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size 
UIGraphicsBeginImageContext(aRect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
UIImage* thumbnailImage; 


NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf); 

for(int i = 0; i < totalNum; 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, i + 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(); 

    //Use thumbnailImage (e.g. drawing, saving it to a file, etc) 

    CGContextRestoreGState(context); 

} 


UIGraphicsEndImageContext();  
CGPDFDocumentRelease(pdf); 

私はこれを克服することができる方法上の任意の提案? CGContextDrawPDFPageは私の問題を解決する前に

答えて

関連する問題