2012-03-22 4 views
1

Мне нужно снять снимок экрана и сохранить в формате pdf. Я выполнил задачу сохранения как PDF, но скриншот, который я беру, всегда дает пустой pdf-файл. Понятия не имею почему. Мой код выглядит следующим образом:Сделайте снимок экрана с экрана

-(IBAction)takeScreenShot 
{ 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
else 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageView *newImage = [[UIImageView alloc] initWithImage:image]; 
UIGraphicsEndImageContext(); 

[self createPDFfromUIView:newImage saveToDocumentsWithFileName:@"SecondScreen1.pdf"]; 
} 


-(void)createPDFfromUIView:(UIImageView*)aView saveToDocumentsWithFileName:(NSString*)aFilename 
{ 
// Creates a mutable data object for updating with binary data, like a byte array 
NSMutableData *pdfData = [NSMutableData data]; 
//CGSize pageSize = CGSizeMake(612, 792); 
// Points the pdf converter to the mutable data object and to the UIView to be converted 
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); 
UIGraphicsBeginPDFPage(); 

// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 
//[aView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

// remove PDF rendering context 
UIGraphicsEndPDFContext(); 

// Retrieves the document directories from the iOS device 
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; 

// instructs the mutable data object to write its context to a file on disk 
[pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 
} 
+0

Этот расширенный дубликат вашего собственного вопроса? http://stackoverflow.com/questions/9724378/can-you-download-something-as-pdf-from-an-iphone-app –

+0

Хорошо, но я столкнулся с проблемой при выполнении задачи. Вышесказанное, насколько я понял из предыдущего вопроса. Но ответ ниже правильный и что сортирует проблему – CodeGeek123

+1

Хорошо, я вижу, что вы ее работаете. Это хорошо! –

ответ

3

Эта строка, которая закомментирована, должна писать ваше изображение в формате pdf. Верните этот код и он может работать.

[aView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

Если это ошибка (без ошибок), убедитесь, что изображение UIImage * не пустое.

+0

Да, это сработало отлично. Спасибо :) – CodeGeek123