I am creating pdf using below code
UIImage *CurImage=[UIImage imageWithContentsOfFile:[ImageArr objectAtIndex:i]];
UIView *ViewDraw=[[UIView alloc]initWithFrame:CGRectMake(0,10, pageSize, pageSize)];
ViewDraw.backgroundColor=[UIColor clearColor];
CGContextBeginPage (pdfContext,nil);
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0,CurImage.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
int y = (pageSize - CurImage.size.height)/2;
int x = (pageSize - CurImage.size.width)/2;
[CurImage drawInRect:CGRectMake( (ViewDraw.frame.width - curImage.frame.width)/2, 24, size.width, curImage.frame.height)];
// CGContextDrawImage(pdfContext, CGRectMake(x,-y, CurImage.size.width, CurImage.size.height), CurImage.CGImage);
CGContextEndPage (pdfContext);
CurImage=nil;
but it is generating upside down i.e.180 degree with mirror image.
What is wrong here?
Turn PDF upside-down:
CGAffineTransform aCgAffTrans = CGAffineTransformMakeTranslation(0,892); //here the whole PDF page height should be given
aCgAffTrans = CGAffineTransformScale(aCgAffTrans, 1.0, -1.0);
CGContextConcatCTM(aCgPDFContextRef, aCgAffTrans);
Your are providing some random height while some part gets turned down and others remains the same. So a mirror like effect appears.
Related
This is a simple issue, which is bugging me ...
So, I have an object of UIImageView over which, I use
-(void)rotate {
// ...
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(180));
self.image.transform = transform; // image is a property.
}
So, when I call this method once, it rotates the object !. When I call the method again, I expect the object to be rotated again - but it is not rotating by another 180 degrees. After messing with the coordinates and moving the image, I can understand that the -perspective of the object has also be rotated by 180 degrees and all coordinates have been inverted as well.
I want the object's perspective to be restored (although it's been rotated by some angle) such that, I can continue to rotate and move it just like before !
Any ideas ?
Anyways, this code works:
UIImage* rotateUIImage(const UIImage* src, float angleDegrees) {
UIView* rotatedViewBox = [[UIView alloc] initWithFrame: CGRectMake(0, 0, src.size.width, src.size.height)];
float angleRadians = angleDegrees * ((float)M_PI / 180.0f);
CGAffineTransform t = CGAffineTransformMakeRotation(angleRadians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM(bitmap, angleRadians);
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Solved the problem with CABasicAnimation !
I am trying to draw contents of scrollview into a PDF context & I am facing problem with pagination.
Following Code I have used:
- (void)renderTheView:(UIView *)view inPDFContext:(CGContextRef)pdfContext
{
// Creating frame.
CGFloat heightOfPdf = [[[self attributes] objectForKey:SRCPdfHeight] floatValue];
CGFloat widthOfPdf = [[[self attributes] objectForKey:SRCPdfWidth] floatValue];
CGRect pdfFrame = CGRectMake(0, 0, widthOfPdf, heightOfPdf);
CGRect viewFrame = [view frame];
if ([view isKindOfClass:[UIScrollView class]])
{
viewFrame.size.height = ((UIScrollView *)view).contentSize.height;
[view setFrame:viewFrame];
}
// Calculates number of pages.
NSUInteger totalNumberOfPages = ceil(viewFrame.size.height/heightOfPdf);
// Start rendering.
for (NSUInteger pageNumber = 0; pageNumber<totalNumberOfPages; pageNumber++)
{
// Starts our first page.
CGContextBeginPage (pdfContext, &pdfFrame);
// Turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0,view.bounds.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
// Calculate amount of y to be displace.
CGFloat ty = (heightOfPdf*(pageNumber));
CGContextTranslateCTM(pdfContext,0,-ty);
[view.layer renderInContext:pdfContext];
// We are done drawing to this page, let's end it.
CGContextEndPage (pdfContext);
}
}
It creates required number of pages but places the content wrongly. Following figure explains it.
Is there anything wrong in my code?
I don't think you need the code which flips the PDF upside down. I've done this before (manual pagination, but without a scroll view) and never had to flip the context vertically. My main concern is that you are doing it on every iteration - if you have a valid reason for flipping it, you probably don't need it in the loop, but just once before the loop begins. Also the code CGContextTranslateCTM(pdfContext,0,-ty); might need to be replaced with CGContextTranslateCTM(pdfContext,0,heightOfPdf);
If that doesn't work, try UIGraphicsBeginPDFPage(); instead of CGContextBeginPage(), that's the only major difference between your code and mine.
This github project can definitely help you.
- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform);
CGContextDrawPDFPage(ctx, page);
}
Found this link to render a PDF, it says about multiple pages but haven't shown an implementation. It doesnt straight away answer to your question but it tells a simpler method to render a pdf with much less translation and transforms.
Render PDF
Generate Multipage PDF
-anoop
You could consider using a UIScrollView to layout your separate PDF pages. The following method loads each PDF page in a view (PDFView) and adds it to the scroll view content in a centered fashion:
- (void) loadPDFAtPath:(NSString *)path
{
NSURL *pdfUrl = [NSURL fileURLWithPath:path];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
float height = 0.0;
for(int i = 0; i < CGPDFDocumentGetNumberOfPages(document); i++) {
CGPDFPageRef page = CGPDFDocumentGetPage(document, i + 1);
PDFView *pdfView = [[PDFView alloc] initPdfViewWithPageRef:page];
CGRect pageSize = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
pv.frame = CGRectMake((self.frame.size.width / 2.0) - pageSize.size.width / 2.0, height, pageSize.size.width, pageSize.size.height);
height += pageSize.size.height + SOME_SPACE_IN_BETWEEN_PAGES;
// self is a subclass of UIScrollView
[self addSubview:pdfView];
[pdfView setNeedsDisplay];
}
CGPDFDocumentRelease(document);
[self setContentSize:CGSizeMake(self.frame.size.width, height)];
}
In this case, PDFView is responsible for rendering a single CGPDFPageRef in its drawRect or drawLayer implementation.
The portions you need to add to pdf pages put in seperate views inside the scroll and render tht view to the pdf context
OR
Just find the correct dimensions and draw using the CG method:
UIImage *Logo=[UIImage imageNamed:#"small-logo-left-top130_133.png"];
CGPoint drawingLogoOrgin = CGPointMake(5,5);
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[Logo drawAtPoint:drawingLogoOrgin];
There are a lot of drawing methods to draw pdf. You can use that to draw all the contents.
this might help you.
- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx
{
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform);
CGContextDrawPDFPage(ctx, page);
}
I want to rotate an UIImage (not UIImageView) in custom degree
I followed this post but it didn't work for me.
Anyone can help? Thanks.
UPDATE:
The code below does some of the job, but I lose some of the image after rotating it:
What should I change to get it right? (btw the yellow color in the screenshots is my UIImageView bg)
- (UIImage *) rotate: (UIImage *) image
{
double angle = 20;
CGSize s = {image.size.width, image.size.height};
UIGraphicsBeginImageContext(s);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0,image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextRotateCTM(ctx, 2*M_PI*angle/360);
CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width, image.size.height),image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This method return you image on your angle of rotate
#pragma mark -
#pragma mark Rotate Image
- (UIImage *)scaleAndRotateImage:(UIImage *)image {
CGImageRef imgRef = image.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
CGFloat boundHeight;
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0); //use angle/360 *MPI
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
- (UIImage *)rotate:(UIImage *)image radians:(float)rads
{
float newSide = MAX([image size].width, [image size].height);
CGSize size = CGSizeMake(newSide, newSide);
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, newSide/2, newSide/2);
CGContextRotateCTM(ctx, rads);
CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage);
//CGContextTranslateCTM(ctx, [image size].width/2, [image size].height/2);
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return i;
}
this function rotates any image on its center, but the image becomes a square so I would suggest referencing the image center when drawing it after this function.
You need to address two things to make this work.
You are rotating about the bottom corner of the image instead of the centre
The bounding rectangle of the resulting image needs to be larger now the image is rotated for it to fit in.
To solve the rotation about the centre, first perform a translate to the centre, then rotate, then translate back.
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);
// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
To calculate the size of the bounding rectangle you'd need to fit the new rotated image into use this:
- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation {
// Calculate the width and height of the bounding rectangle using basic trig
CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation));
CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation));
// Calculate the position of the origin
CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth) / 2);
CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight) / 2);
// Return the rectangle
return CGRectMake(newX, newY, newWidth, newHeight);
}
You can find these techniques in my previous posts and answers here:
Creating a UIImage from a rotated UIImageView
and here:
Saving 2 UIImages
Hope this helps,
Dave
for rotate image.. you can use this IBAction ... for each and every button click, the image will be rotate by 90 degree...
-(IBAction)rotateImageClick:(id)sender{
UIImage *image2=[[UIImage alloc]init];
image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree
self.roateImageView.image = image2;
imgData= UIImageJPEGRepresentation(image2,0.9f);
}
for rotating image u only have to pass UIimage and rotating degrees for the following method
- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees
//------------------------------------------------------------------------
#pragma mark - imageRotatedByDegrees Method
- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// // Rotate the image context
CGContextRotateCTM(bitmap, (degrees * M_PI / 180));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I think this link will be help.......! Rotate Original Image by clicking button in Objective C
http://adrianmobileapplication.blogspot.com/2015/03/rotate-original-image-by-clicking.html
You have to do some thing like this
YourContainer.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI );
I think rest of the thing you can figured out..
hi i am trying to print pdf,It is printing well but the problem is not fit the width for every pdf.my code is like this..
//for scalling
-(CGSize)getPageFitSizeOfSize:(CGSize)sourceSize inSize:(CGSize)containerSize{
// now fit the source to the container size - compute the width and height of the //image once it is aspect fit
// first fit it to the height
CGFloat heightWhenHeightIsFit = containerSize.height;
CGFloat widthWhenHeightIsFit = (sourceSize.width * heightWhenHeightIsFit)/sourceSize.height;
CGFloat heightWhenAspectFit = heightWhenHeightIsFit;
CGFloat widthWhenAspectFit = widthWhenHeightIsFit;
if(widthWhenAspectFit > containerSize.width){
widthWhenAspectFit = containerSize.width;
heightWhenAspectFit = (sourceSize.height *widthWhenAspectFit)/sourceSize.width;
}
return CGSizeMake(widthWhenAspectFit, heightWhenAspectFit);
}
//printing
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
int c=[currentPage intValue]+1;
NSLog(#"drawing started");
if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){
myPageRef = CGPDFDocumentGetPage(myDocumentRef, c);
CGSize pageSize = [kbDataSource pageSize];
GLogInfo(#"the page ize we are getting in draw layer is %# ",NSStringFromCGSize(pageSize));
CGContextSaveGState(ctx);
CGSize aspectFitSize = [self getPageFitSizeOfSize:pageSize inSize:CGSizeMake (self.view.bounds.size.width, self.view.bounds.size.height- __TOP_BRANDING_BAR_HEIGHT)];
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
NSLog(#"the CGContextGetClipBoundingBox method rect %#",NSStringFromCGRect(CGContextGetClipBoundingBox(ctx)));
CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
GLogDebug(#"aspect fitsize of image to %#", NSStringFromCGSize(aspectFitSize));
GLogDebug(#"layer bounds are:%# %# ",NSStringFromCGSize(layer.bounds.size),NSStringFromCGPoint(layer.bounds.origin));
NSLog(#"page size of the book is:%#",NSStringFromCGSize(pageSize));
CGFloat aspectRatio=pageSize.width/pageSize.height;
NSLog(#"the value of aspect ratio is %f ",aspectRatio);
CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
NSLog(#"the crop box values %#",NSStringFromCGRect(cropBox));
CGRect targetRect = layer.bounds;
GLogDebug(#"the targetRect is %#",NSStringFromCGRect(targetRect));
CGFloat xScale = targetRect.size.width / cropBox.size.width;
CGFloat yScale = (targetRect.size.height-41) / cropBox.size.height;
CGFloat scaleToApply = (xScale < yScale) ? xScale : yScale;
NSLog(#"the scaleToApply is %f,X-scale is %f,Y-scale is %f",scaleToApply,xScale,yScale);
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
NSLog(#"the view bounds are %#",[self.view description]);
if (scaleToApply == yScale){
NSLog(#"the values of CGAffineTransformMakeTranslation in if condition is %# ",NSStringFromCGAffineTransform(CGAffineTransformMakeTranslation(-100, -150)));
CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-100, -150));
}
else{
CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-180, -260)); //evans
NSLog(#"the values of CGAffineTransformMakeTranslation in else condition is %# ",NSStringFromCGAffineTransform(CGAffineTransformMakeTranslation(-180, -260)));
}
NSLog(#"the affineTransform makescale after applaying scaleToApplay %#",NSStringFromCGAffineTransform(CGAffineTransformMakeScale(scaleToApply, scaleToApply)));
CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
GLogDebug(#"the CGPDFPageGetDrawingTransform values are %#",NSStringFromCGAffineTransform(CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true)));
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
//
//CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);
CGContextDrawPDFPage(ctx, myPageRef);
CGContextRestoreGState(ctx);
[loadingIndicator stopAnimating];
[loadingIndicator1 stopAnimating];
[loadingIndicator2 stopAnimating];
}
i am trying to fit all pdfs width but it is not working..i posted many questions regarding this and searched in net aslo no use can any one please give me the exact scaling.
thank you all.
There is a pretty easy way to get the rect of a PDF page,
CGRect pdfPageRect = CGPDFPageGetBoxRect(CGPDFDocumentGetPage(myDocumentRef,pageNumber), kCGPDFMediaBox);
Three things will effect the size of the drawn PDF:
CGContextTranslateCTM(ctx, 0.0,
HEIGHT);//HEIGHT
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(p,kCGPDFCropBox,MY_RECT, 0,true));//MY_RECT
CALayer.tileSize
The Scale can also be effected by the two numbers in this function: CGContextScaleCTM(ctx, 1, -1);
I'm trying to render a CGPDFPage (selected from a CGPDFDocument) into a UIImage to display on a view.
I have the following code in MonoTouch which gets me part way there.
RectangleF PDFRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
public override void ViewDidLoad ()
{
UIGraphics.BeginImageContext(new SizeF(PDFRectangle.Width, PDFRectangle.Height));
CGContext context = UIGraphics.GetCurrentContext();
context.SaveState();
CGPDFDocument pdfDoc = CGPDFDocument.FromFile("test.pdf");
CGPDFPage pdfPage = pdfDoc.GetPage(1);
context.DrawPDFPage(pdfPage);
UIImage testImage = UIGraphics.GetImageFromCurrentImageContext();
pdfDoc.Dispose();
context.RestoreState();
UIImageView imageView = new UIImageView(testImage);
UIGraphics.EndImageContext();
View.AddSubview(imageView);
}
A section of the CGPDFPage is displayed but rotated back-to-front and upside down. My question is, how do I select the full pdf page and flip it round to display correctly. I have seen a few examples using ScaleCTM and TranslateCTM but couldn't seem to get them working.
Any examples in ObjectiveC are fine, I'll take all the help I can get :)
Thanks
I haven't worked with MonoTouch. However, in objective-C you would get an image for a PDF page like this (notice the CTM transforms):
-(UIImage *)getThumbForPage:(int)page_number{
CGFloat width = 60.0;
// Get the page
CGPDFPageRef myPageRef = CGPDFDocumentGetPage(myDocumentRef, page);
// Changed this line for the line above which is a generic line
//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;
}