2012-03-29 78 views
0

我使用從我的Core Data圖中輸出的數據即時生成PDF文檔,但事實證明這非常困難。使用Core Data中的CGRect繪製UIImage

我使用的素材資源庫以獲取圖像的文件路徑從核心數據圖的使用以下:

NSURL *url = [[NSURL alloc] initWithString:bird.photo]; 
     ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; 

     [library assetForURL:url resultBlock:^(ALAsset *asset) { 

        _pdfBirdmage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]; 


       } failureBlock:^(NSError *error) { 

        NSLog(@"Couldn't load asset %@ => %@", error, [error localizedDescription]); 

       }]; 

,這是顯示圖像是爲以下幾點:

birdphotoview = <UIImage: 0x1caac0> 

很明顯,我想這是一種圖像格式,如PNG或JPG,以便將其寫入PDF文檔,使用下面的代碼:

[_pdfBirdImage drawInRect:CGRectMake((pageSize.width - _pdfBirdImage.size.width/2)/2, 350, _pdfBirdImage.size.width/2, _pdfBirdImage.size.height/2)]; 

但是,這不是打印任何東西的pdf。

任何幫助將是輝煌的。

+0

圖像是否正確?你是否試圖在圖像視圖中顯示它?您的pdf繪圖環境是否正確? – cocoakomali 2012-03-29 17:19:32

+0

是的,是的,是的..更嚴重的說明,我認爲UIImage必須是.png圖像。基本上,我需要將我的轉換爲.png – jcrowson 2012-03-29 19:01:13

+0

要執行轉換使用UIImagePNGRepresentation(UIImage *圖像)函數,它返回一個NSData對象。將其寫入文件並使用該圖像。 – cocoakomali 2012-03-30 03:34:44

回答

0

您是否使用基於CGPDF的例程?在這種情況下,我認爲Y方面是相反的。我之前已經顯示過PDF,並將數據覆蓋在頂部(隨後將整個屏幕呈現回磁盤,但是作爲圖像)。如果是這種情況,我必須首先轉換現有的PDF。聽起來不像你的問題,但我沒有看到這裏引用的任何PDF代碼,很難說。無論如何,一直是:

UIGraphicsBeginImageContext(pdfPageRectScaled.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

// First fill the background with white. 
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); 
CGContextFillRect(context, pdfPageRectScaled); 

CGContextSaveGState(context); 
// Flip the context so that the PDF page is rendered 
// right side up. 
CGContextTranslateCTM(context, 0.0f, pdfPageRectScaled.size.height); 
CGContextScaleCTM(context, 1.0f, -1.0f); 

// Scale the context so that the PDF page is rendered 
// at the correct size for the zoom level. 
CGContextScaleCTM(context, pdfScale, pdfScale); 
CGContextDrawPDFPage(context, page); 
CGContextRestoreGState(context);