CGPDFPageDrawingTransform not Rotating the Page - iphone

The code below is intended to display the pages of a PDF while properly handling any non-zero rotation angles that might be specified for each page. My test PDF file has multiple pages and one them has a 180 degree rotation angle, which the code properly detects, but the call to CGPDFPageGetDrawingTransform (followed by CGContextContactCTM) has no effect. The page is being displayed unrotated. What am I doing wrong?
CGContextSaveGState(context);
CGRect drawRect=self.bounds;
CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
int rotationAngle=CGPDFPageGetRotationAngle(pdfPage);
if (kDebug>1) {
NSLog(#"***** page rotation angle is %d",rotationAngle);
}
CGContextTranslateCTM(context, drawRect.origin.x, drawRect.origin.y);
CGAffineTransform transform = CGPDFPageGetDrawingTransform(pdfPage, kCGPDFCropBox, cropBox, rotationAngle, true);
CGContextConcatCTM (context, transform);
float xScaleFactor=drawRect.size.width / cropBox.size.width;
float yScaleFactor=drawRect.size.height / cropBox.size.height;
CGContextTranslateCTM(context, -CGRectGetMinX(cropBox), CGRectGetMaxY(cropBox)*yScaleFactor);
CGContextScaleCTM(context, xScaleFactor, -yScaleFactor);
CGContextClipToRect(context, cropBox);
CGContextDrawPDFPage(context, pdfPage);
CGContextRestoreGState(context);
Thanks,
//Scott

So after a careful re-reading of the API for CGPDFPageGetDrawingTransform, I discover that the value of page's /ROTATE attribute is ADDED to the rotation angle you specify in the method call. In my situation above, the page's /ROTATE happend to be 180. was grabbing that in the rotationAngle var and passing that in to get the transform. So, 180+180 = 360 = no visibile rotation.
I now call GPDFPageGetDrawingTransform with a rotation angle of 0.0. Anything in the /ROTATE is added, and voila, I get the correct results.

Related

Rotate CGRect property of a CCSprite ?

I have a sublclass of a CCSprite that rotates throughout the game and there is a shield that has to rotate around the sprite according to the sprite's rotation. So if the sprite's rotation is 75 degrees there should be a CGRect located at 75 degrees. The dimensions of the CGRect are subordinate as it almost resembles a square.
What I did is:
I subclassed CCSprite and added a property called shieldArea.
Upon initialization I set this rect to be
self.shieldArea = CGRectMake(self.position.x-30, self.position.y, 8, 10);
Then I rotate the sprite itself, however, the rect stays at its initial position.
I hoped that the CGrect would be affected by the rotation, but I kind of expected it not to affect it, of course, why should it ? So, my question is, how do I rotate a CGRect at all ? Or do I have to add a new CGRect all the time ?
Side notes: I do not want to use Box2d or anything the like. I handle collision detection myself.
Have you tried CGAffineTransform?
Something like this:
float centerX = myOldRect.origin.x + (myOldRect.size.width / 2.0);
float centerY = myOldRect.origin.y + (myOldRect.size.height / 2.0);
CGAffineTransform rotation = CGAffineTransformMakeRotation(someAngleInRadians);
CGAffineTransform moveAnchor = CGAffineTransformMakeTranslation(centerX, centerY);
CGAffineTransform centeredRotation = CGAffineTransformConcat(moveAnchor, rotation);
CGRect rotatedRect = CGRectApplyAffineTransform(myOldRect, centeredRotation);
Note, this is NOT tested. Use at your own risk :p

Box2d fixture and body out of sync on retina display

I'm trying to make a cocos2d/box2d game work on iPad, iPhone and iPhone retina.
My problem is, that the fixture and body don't line up on the retina simulator, please click on screenshots below for illustration (as a new stackoverflow member, it won't allow me to post the screenshot here).
screenshot
(please disregard the different shapes, I want the 4 corners to line up)
I've done quite a bit of research on this over the last couple of days, and the closest I found was this:
link
But the solution offered there with PTM_RATIO and CC_CONTENT_SCALE_FACTOR() doesn't seem to work in my case. I think it has to do with the fact that I don't load an image from file into my sprite. Most solutions to this problem are based on loading -hd image files for the retina display, but I don't want to use files in my game at all. I basically want to draw the polygons myself at runtime,
My code looks as follows:
-(CCSprite*)addSprite
{
CGSize contextsize = CGSizeMake(200, 200);
UIGraphicsBeginImageContext(contextsize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFlush(context);
CGContextSetAllowsAntialiasing(context, true);
CGContextTranslateCTM(context, 0, contextsize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
UIBezierPath* aPath;
aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)
radius:100
startAngle:0
endAngle:1.57
clockwise:YES];
[aPath addArcWithCenter:CGPointMake(100, 100)
radius:50
startAngle:1.57
endAngle:0
clockwise:NO];
[aPath stroke];
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
UIImage *graphImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:graphImage] autorelease];
CCSprite *sprite = [CCSprite spriteWithTexture:tex];
return sprite;
}
-(void) addFixture:(CCSprite *)fixsprite
{
b2Vec2 arcdots[] = {
b2Vec2(50.0f / PTM_RATIO, 0.0f / PTM_RATIO),
b2Vec2(100.0f / PTM_RATIO, 0.0f / PTM_RATIO),
b2Vec2(0.0f / PTM_RATIO, 100.0f / PTM_RATIO),
b2Vec2(0.0f / PTM_RATIO, 50.0f / PTM_RATIO)
};
b2PolygonShape p_shape;
b2FixtureDef fixtureDef;
b2BodyDef bodyDef;
bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
bodyDef.userData = fixsprite;
b2Body *body = world->CreateBody(&bodyDef);
p_shape.Set(arcdots, 4);
fixtureDef.shape = &p_shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
And I call these functions from the main routine as follows:
CCSprite *sprite2 = [self addSprite];
sprite2.position = ccp(0, 0);
[self addChild:sprite2 z:0];
[self addFixture:sprite2];
I have these lines uncommented in the delegate file:
if( ! [director enableRetinaDisplay:YES] )
CCLOG(#"Retina Display Not supported");
Please let me know if further information is required. And please be gentle, I'm only starting to learn this. Thanks for your time.
Unless otherwise mentioned, all coordinates in cocos2d (and most of UIKit) are given in points, not pixels. On a Retina display device you still have a point resolution of 480x320 points (960x640 pixels).
From that follows: when you calculate in actual pixels, multiply or divide by CC_CONTENT_SCALE_FACTOR. If you deal with point coordinates, do nothing. Since you're rendering your own polys I assume you know whether you use actual pixel coordinates or not. If you use OpenGL directly, then you'll be working with pixel coordinates.
I'm not sure if enabling Retina display mode does anything for you if you don't use cocos2d to render your content.
Lastly, a common misunderstanding is that the Box2D world is using point coordinates and must be transformed to pixels or vice versa. Neither is the case. The Box2D world is completely oblivious to a specific coordinate system. The use of PTM_RATIO is done only to ensure that Box2D coordinates are within reasonable ranges for the Box2D engine, since it works best with objects that are 1 meter in size/diameter, and most objects should range from 0.1 to 10 meters in diameter.

IOS, how to clear context graphics

In my app I have a method that draws a pdf into a context:
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFTrimBox),
CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform);
CGContextDrawPDFPage(ctx, page);
Now in drawLayer, that is called when zooming, I do the necessary transformations and call again CGContextDrawPDFPage(ctx, page);
What happens is that a zoomed pdf is drawn on top of the first pdf, the problem is that in a particular page with only text, the back and blurred pdf is shown. That is strange, I thought that pdf page had white background and if this happens it's because the zoomed pdf on top has transparent background.
Now, to solve this how can I clear the context right before the CGContextDrawPDFPage(ctx, page) of drawLayer method? I tried:
//self.view.transform = CGAffineTransformIdentity;
//CGAffineTransform transform = CGAffineTransformIdentity;
//CGContextConcatCTM(ctx, transform);
//CGContextClearRect(ctx, layer.bounds);
Nothing works...thanks in advance
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);
Did you try to flush the context as below?
CGContextFlush(ctx);

PDF-Scrollview - show PDF - document in landscape format

I'm using Apple's example «PDFScrollView» to display PDFs on an iPad. This works fine, as long as the PDF-document is in Portrait-Mode.
When the document is in Landscape-mode, though, the document is always shown rotated by 90 degrees.
I found this How to detect the orientation of a PDF document in iPhone SDK - but when I try to get the dimensions of the PDF, the height is always bigger than the width, no matter what orientation the PDF has...
Any ideas?
As mentionend in, the CGPDFPageGetBoxRect(page, kCGPDFMediaBox) always returns the same size for a PDF, no matter what orientation the PDF has.
But the rotation of the page is returned correctly.
So I get the rotation using this code:
rotate = CGPDFPageGetRotationAngle(page);
And then use the code below which I found here: http://ipdfdev.com/2011/03/23/display-a-pdf-page-on-the-iphone-and-ipad/
switch (rotate) {
case 0:
// Translate the origin of the coordinate system at the
// bottom left corner of the page rectangle.
CGContextTranslateCTM(context, 0, cropBox.size.height);
// Reverse the Y axis to grow from bottom to top.
CGContextScaleCTM(context, 1, -1);
break;
case 90:
// Reverse the Y axis to grow from bottom to top.
CGContextScaleCTM(context, 1, -1);
// Rotate the coordinate system.
CGContextRotateCTM(context, -M_PI / 2);
break;
case 180:
case -180:
// Reverse the Y axis to grow from bottom to top.
CGContextScaleCTM(context, 1, -1);
// Translate the origin of the coordinate system at the
// top right corner of the page rectangle.
CGContextTranslateCTM(context, cropBox.size.width, 0);
// Rotate the coordinate system with 180 degrees.
CGContextRotateCTM(context, M_PI);
break;
case 270:
case -90:
// Translate the origin of the coordinate system at the
// bottom right corner of the page rectangle.
CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
// Rotate the coordinate system.
CGContextRotateCTM(context, M_PI / 2);
// Reverse the X axis.
CGContextScaleCTM(context, -1, 1);
break;
}
Et voilà - the PDF is displayed in the right orientation
I am writing this with assumption that your problem is about rotating document when user rotates device....
For that you have to redraw document in view. TO achieve that do following....
change frame of the view in which document is drawn, and CALayer(OR CATiledLayer) being used in that view in following method.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Call setNeedsDisplay method of CATiledLayer after changing frame.
Then return YES in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation.
Above steps will redraw the document in new frame of you View accordingly so it will appear properly.
->IF your view is in full screen or then only step 2 ans 3 will be enough.
Post here if any further assistance is required....
Is there a particular reason you are drawing PDFs yourself? You could use QLPreviewController on iOS 4.0 or later, and UIDocumentInteractionController for previous versions.

Save a UIImage from a UIImageView with CGAffineTransform

I have a UIImageView within a UIScrollView which I have enabled the user to perform any number of flip and rotation operations on. I have this all working which allows the user to zoom, pan, flip and rotate. Now I want to be able to save the final image out to a png.
however it is doing my head in trying to work this out...
I have seen quite a few other posts similar to this but most only require applying a single transform such as a rotation eg Creating a UIImage from a rotated UIImageView
I would like to apply any transform that the user has "created" which will be a series of flip and rotations concatenated togethers
As the user is applying various rotations, flips etc, I store the concatenated transform using CGAffineTransformConcat. For example when they rotate I do:
CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle);
self.theFullTransform = CGAffineTransformConcat(self.theFullTransform, newTransform);
self.fullPhotoImageView.transform = self.theFullTransform;
The following method is the best I have gotten so far for creating a UIImage with the full transform however the image is always translated in the wrong place. Eg the image is "offset". Which my guess is either related to using the wrong bounds being set in in CGAffineTransformTranslate or CGContextDrawImage.
Does anyone have any ideas? This seems a lot harder that I thought it should be...
- (UIImage *) translateImageFromImageView: (UIImageView *) imageView withTransform:(CGAffineTransform) aTransform
{
UIImage *rotatedImage;
// Get image width, height of the bounding rectangle
CGRect boundingRect = CGRectApplyAffineTransform(imageView.bounds, aTransform);
// Create a graphics context the size of the bounding rectangle
UIGraphicsBeginImageContext(boundingRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = CGAffineTransformIdentity;
//I think this translaton is the problem?
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
transform = CGAffineTransformConcat(transform, aTransform);
CGContextConcatCTM(context, transform);
// Draw the image into the context
// or the boundingRect is incorrect here?
CGContextDrawImage(context, boundingRect, imageView.image.CGImage);
// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
// Clean up
UIGraphicsEndImageContext();
return rotatedImage;
}
Is the offset predictable, like always half the image, or does it depend on aTransform?
struct CGAffineTransform {
CGFloat a, b, c, d;
CGFloat tx, ty;
};
If the latter, set tx and ty to zero in aTransform before using it.