I use the following code to draw and rotate a PDF file. It works perfectly fine with iOS 5.x, but under iOS 4.3 just a white page shows up with an error in the debugger:
invalid `Contents': not an array of streams.
The error takes place right after "CGContextDrawPDFPage(context, pdfPage);"
Why does it work with iOS 5.x but does not 4.3.x? I tried different PDF files, but still I got the same result.
How do I overcome this issue?
pdfpage is defined as:
- (void)setPage:(CGPDFPageRef)newPage
{
CGPDFPageRelease(self->pdfPage);
self->pdfPage = CGPDFPageRetain(newPage);
}
the method where it happens:
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
// First fill the background with white.
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,self.bounds);
CGContextSaveGState(context);
int rotate = CGPDFPageGetRotationAngle(pdfPage);
switch (rotate) {
case 0:
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1, -1);
break;
case 90:
CGContextScaleCTM(context, 1.0, -1.0);
CGContextRotateCTM(context, -M_PI / 2);
break;
case 180:
case -180:
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, self.bounds.size.width, 0);
CGContextRotateCTM(context, M_PI);
break;
case 270:
case -90:
CGContextTranslateCTM(context, self.bounds.size.height, self.bounds.size.width);
CGContextRotateCTM(context, M_PI / 2);
CGContextScaleCTM(context, -1, 1);
break;
}
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextScaleCTM(context, myScale,myScale);
CGContextDrawPDFPage(context, pdfPage); // the error shows up right after executing this line
CGContextRestoreGState(context);
}
According to PDF specification, the /Contents entry in the page dictionary can be either a stream object or an array of stream objects. Based on the error message, it seems the iOS 4.3 does not properly implement the PDF specification, it always expects an array of streams for the /Contents entry and your files use a single stream object. iOS 5 probably fixed this problem.
Try to do CGPDFDocumentRetain(yourCGPDFDocumentRef) at first.
Related
I followed another StackOverflow post that explains how i could override the draw method of a PDFAnnotation so i could draw a picture instead of a traditional PDFAnnotation.
But sadly i was not able to achieve that and the annotation that is drawn on top of my pdf is still a regular one.
This is the code that i used :
#implementation PDFImageAnnotation { UIImage * _picture;
CGRect _bounds;};
-(instancetype)initWithPicture:(nonnull UIImage *)picture bounds:(CGRect) bounds{
self = [super initWithBounds:bounds
forType:PDFAnnotationSubtypeWidget
withProperties:nil];
if(self){
_picture = picture;
_bounds = bounds;
}
return self;
}
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
[_picture drawInRect:_bounds];
CGContextRestoreGState(context);
UIGraphicsPushContext(context);
};
#end
Does someone know how i could override the draw method so i could draw a custom Annotation ?
Thank You !
ps: i also tried to followed the tutorial on the apple dev site.
UPDATE :
Now i'm able to draw pictures using CGContextDrawImage but i'm not able to flip coordinates back in place. when i do that mi pictures are not drawn and it seems that they are put outside of the page but i'm not sure.
This is my new code :
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, _pdfView.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, _bounds, _picture.CGImage);
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
I also tried to follow the tutorial on the Apple dev site.
Which one?
Custom Graphics
Adding Custom Graphics to a PDF
Because both include UIGraphicsPushContext(context) & CGContextSaveGState(context) calls, but your code doesn't. Do not blindly copy & paste examples, try to understand them. Read what these two calls do.
Fixed code:
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
[_picture drawInRect:_bounds];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
The image was drawn with CGRectMake(20, 20, 100, 100). It's upside down, because PDFPage coordinates are flipped (0, 0 = bottom/left). Leaving it as an exercise for OP.
Rotation
Your rotation code is wrong:
CGContextTranslateCTM(context, 0.0, _pdfView.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, _bounds, _picture.CGImage);
It's based on _pdfView bounds, but it should be based on the image bounds (_bounds). Here's the correct one:
- (void)drawWithBox:(PDFDisplayBox) box
inContext:(CGContextRef)context {
[super drawWithBox:box inContext:context];
UIGraphicsPushContext(context);
CGContextSaveGState(context);
CGContextTranslateCTM(context, _bounds.origin.x, _bounds.origin.y + _bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[_picture drawInRect:CGRectMake(0, 0, _bounds.size.width, _bounds.size.height)];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
I am using PDFKit and create a subclass of PDFAnnotation, I want to set my custom padding when I create a free text annotation, so I try to override drawRect function as below
- (void)drawWithBox:(PDFDisplayBox)box inContext:(CGContextRef)context {
UIGraphicsPushContext(context);
CGContextSaveGState(context);
NSAttributedString * mystring = [[NSAttributedString alloc] initWithString:self.contents attributes:#{
NSFontAttributeName: self.font,
NSForegroundColorAttributeName: UIColor.blackColor }];
[mystring drawInRect:CGRectMake(self.bounds.origin.x + 10, self.bounds.origin.y + 10, self.bounds.size.width-20, self.bounds.size.height-20)];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}
The padding works well but the text is upside down.
I try to add the context flipping code based on this link https://forums.developer.apple.com/thread/103683
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
after the line CGContextSaveGState(context);
But it will make the text disappear! Anyone know the reason or meet the similar issue? Thanks!
I have found the answer, in case someone meets the same problem. Actually the CGContextTransform are based on the page coordinate system, so when you use transform CGContextScaleCTM(context, 1.0, -1.0); the context is outside the page range. What we need to get is X1 = X2; Y1 + Y2 = 2 * (self.bounds.origin.y + self.bounds.size.height / 2), so apply transform CGContextConcatCTM(context, CGAffineTransformMake(1, 0, 0, -1, 0.0, 2 * self.bounds.origin.y + self.bounds.size.height)); works for me.
I'm currently using OpenGL ES 2.0 under GLKView on the new iPad. It seems, whenever I make the call to glColor4f, nothing happens (ie. it doesn't color the polygons I want it to) other than causing a GL_INVALID_OPERATION error. Then, as soon as I try to load a texture, I get this error message:
Error loading file: The operation couldn't be completed. (GLKTextureLoaderErrorDomain error 8.)
But before the glColor4f is called, everything works fine. Here's my code below for drawInRect:
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Render the object with GLKit
self.effect.texture2d0.enabled = NO;
self.effect.transform.modelviewMatrix = _defaultModelMatrix;
[self.effect prepareToDraw];
// Render the grid
glBindBuffer(GL_ARRAY_BUFFER, _gridVertexBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, TRIANGLES * 6);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
If anyone can help, I'd really appreciate it. Some things to point out though. The object I'm trying to color is stored in a VBO and is rendered fine, in a white color (even though I'm trying to color it red).
Thanks
glColor4f is not an OpenGLES2.0 command (it's from GLES1.1)
What you want to do is declare a uniform in your shader called 'color' (or something similar), set that uniform as you typically would with any shader uniform, and multiply your fragment color with that uniform before writing the pixel.
I've made methods like follow and excute code.
I am confusing about origin of quartz framework after looking follow result image.
Please look at follow code and image.
[in ViewController.m]
Excute a follow method when press UIButton.
- (void)test {
[customView drawCustom];
[customView setNeedsDisplay];
}
[in CustomView.m]
- (void)drawRect:(CGRect)rect {
cgc_Context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(cgc_Context, 5.0);
CGContextSetStrokeColorWithColor(cgc_Context, [UIColor redColor].CGColor);
CGContextMoveToPoint(cgc_Context, 0, 0);
CGContextAddLineToPoint(cgc_Context, 200, 50);
CGContextStrokePath(cgc_Context);
}
- (void)drawCustom {
CGContextSetLineWidth(cgc_Context, 5.0);
CGContextSetStrokeColorWithColor(cgc_Context, [UIColor yellowColor].CGColor);
CGContextMoveToPoint(cgc_Context, 0, 0);
CGContextAddLineToPoint(cgc_Context, 200, 50);
CGContextStrokePath(cgc_Context);
}
[Result screen]
Red Line : result of drawRect
Yellow Line : result of drawCustom
Why does occur different origin point?
You must read the apple's documentation about Quartz 2D Coordinate Systems
I've got a problem with OpenGL on mac, and I think the problem is the Depth test.
So, to my problem: Rather than explaining, I made two screenshots:
My scene from far: http://c0848462.cdn.cloudfiles.rackspacecloud.com/dd2267e27ad7d0206526b208cf2ea6910bcd00b4fa.jpg
And from near: http://c0848462.cdn.cloudfiles.rackspacecloud.com/dd2267e27a561b5f02344dca57508dddce21d2315f.jpg
If I do not draw the green floor, everything looks (kinda) fine. But still, like this it looks just aweful.
Here are the three codeblocks I use to set up Opengl:
+ (NSOpenGLPixelFormat*) defaultPixelFormat
{
NSOpenGLPixelFormatAttribute attributes [] = {
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16,
(NSOpenGLPixelFormatAttribute)nil
};
return [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
}
- (void) prepareOpenGL
{
NSLog(#"Preparing OpenGL");
glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
glEnable(GL_TEXTURE_2D);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
- (void)reshape
{
NSLog(#"Reshaping view");
glViewport( 0, 0, (GLsizei)[self bounds].size.width, (GLsizei)[self bounds].size.height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, [self bounds].size.width / [self bounds].size.height, 0.1f /*Nearest render distance*/, 5000.0 /*Render distance*/);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
gluPerspective( 45.0, [self bounds].size.width / [self bounds].size.height, 0.1f /*Nearest render distance*/, 5000.0 /*Render distance*/);
That's way too small of a near clip plane. The closer your near clip value is to 0, the less precision you get on values that are farther away. Push your near clip back to at least 1.0, if not farther. In general, you should push it back as far away as you can live with.
Oh, and you should be using a 24-bit depth buffer, not 16-bit.