8

I have a problem related to pdf drawing in iPhone. When I draw the pdf in ipad it works fine but in iphone quality of image of page is not good. That image contains dark spots at the place of gray background.
Can anyone help me to solve this problem?

thanks.

- (UIImage *) imageForPageIndex:(NSUInteger)pageIndex {
    UIImage *image = nil;
    if ([delegate illustration_Enable])
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, 
                                                     pageSize.width, 
                                                     pageSize.height, 
                                                     8,                     /* bits per component*/
                                                     pageSize.width * 4,    /* bytes per row */
                                                     colorSpace, 
                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
        CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width, pageSize.height));

        [self renderPageAtIndex:pageIndex OnContext:context];

        CGImageRef cgimage = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        image = [UIImage imageWithCGImage:cgimage];
        CGImageRelease(cgimage);
    }
    else 
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, 
                                                     pageSize.width, 
                                                     pageSize.height, 
                                                     8,                     /* bits per component*/
                                                     pageSize.width * 4,    /* bytes per row */
                                                     colorSpace, 
                                                     kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);
        CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width, pageSize.height));

        [self renderPageAtIndex:pageIndex OnContext:context];


        CGImageRef cgimage = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        image = [UIImage imageWithCGImage:cgimage];

        CGImageRelease(cgimage);
    }


    return image;

}

-(void)renderPageAtIndex:(int)index OnContext:(CGContextRef)ctx{

    if ([delegate illustration_Enable])
    {
        if (index-1 == [imageList count]) {
            return;
        }
        UIImage *image = nil;
        image = [UIImage imageWithContentsOfFile:[imageList objectAtIndex:index-1]];
        CGRect rect1 = CGRectMake(0, 0, image.size.width, image.size.height);
        CGRect rect2 = CGContextGetClipBoundingBox(ctx);
        CGAffineTransform transform = [self aspectFill:rect1 :rect2];
        CGContextConcatCTM(ctx, transform);
        CGContextDrawImage(ctx, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);  

    }
    else 
    {
        CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index);
        CGRect rect1 = CGPDFPageGetBoxRect(page,kCGPDFMediaBox);
        CGRect rect2 = CGContextGetClipBoundingBox(ctx);
        CGAffineTransform transform;
        if (takeBookmarkImg)
        {
            transform = [self aspectFill:rect1 :rect2];
        }
        else 
        {
            transform = [self aspectFit:rect1 :rect2];
        }
        CGContextConcatCTM(ctx, transform);
        CGContextDrawPDFPage(ctx, page);
    }



}

- (CGAffineTransform) aspectFit:(CGRect)innerRect :(CGRect) outerRect {

        scaleFactor = MIN(outerRect.size.width/innerRect.size.width, outerRect.size.height/innerRect.size.height);
        baseScaleFactor = scaleFactor;
        CGAffineTransform scale = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
        CGRect scaledInnerRect = CGRectApplyAffineTransform(innerRect, scale);

        CGAffineTransform translation = 
        CGAffineTransformMakeTranslation((outerRect.size.width - scaledInnerRect.size.width) / 2 - scaledInnerRect.origin.x-(totalMoveX+moveX), 
                                         (outerRect.size.height - scaledInnerRect.size.height) / 2 - scaledInnerRect.origin.y+(totalMoveY+moveY));
        return CGAffineTransformConcat(scale, translation);
}

.

3
  • 2
    Could you show us your code that you think is inadequate?
    – johnluttig
    Dec 28, 2011 at 7:27
  • 1
    i have given my code and i hope it will be help full thanks for responding. @johnluttig Dec 28, 2011 at 11:15
  • 1
    Can you post a picture showing the difference between the iPad and iPhone versions? It might be a color space issue, or it could be something else entirely. Jan 3, 2012 at 6:50

1 Answer 1

5

Just a wild guess as it is hard to say without some actual screenshots, but you do not use CGContextSetInterpolationQuality in your render code. Maybe CGContextSetInterpolationQuality(your_context, kCGInterpolationHigh) fixes the problem.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.