Как создать преобразованное изображение в PDF из UIView?
Я использую приведенный ниже код для создания PDF
из UIView
у которого уже есть подпредставления. Я получаю все подпредставления и перерисовываю в контексте PDF. Но я не могу преобразовать UIImageView
, Ниже мой код
NSMutableData *pdfData = [NSMutableData data];
CGRect pageFrame = CGRectMake(0, 0, aView.frame.size.width, aView.frame.size.height);
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
if([view isKindOfClass:[UIImageView class]]) {
UIImageView *imgv = (UIImageView *)view;
[imgv.image drawInRect:CGRectMake(view.frame.origin.x + imgv.frame.origin.x, view.frame.origin.y + imgv.frame.origin.y, imgv.frame.size.width, imgv.frame.size.height)];
}
UIGraphicsEndPDFContext();
Даже если изображение вращается в моем UIView
но PDF не показывает вращение. Как мне превратить его на 90 градусов в pdf
контекст?
2 ответа
Если вам просто нужно нарисовать какой-то элемент с вращением, попробуйте использовать функции преобразования матрицы для текущего контекста (это будет контекст PDF).
CGContextRotateCTM(currentContext, M_PI / 2);
Обратите внимание, что следующий метод создает только растровое изображение представления; это не создает фактическую типографику.
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// 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);
}
Также убедитесь, что вы импортируете: QuartzCore/QuartzCore.