Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retaining image quality while drawing high resolution pdf to iphone?

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);
}

.

like image 306
Abhishek Mishra Avatar asked Dec 28 '11 06:12

Abhishek Mishra


People also ask

How do I save an image as a PDF without losing quality?

To preserve image quality, you'll need a higher DPI. Save your changes then download the document as a PDF. Alternatively, save your Word document as a PDF and change DPI in the advanced settings before you download it.

Why do images get blurry when converting to PDF?

Incorrect Settings If this happens, check your output settings. Some PDF converters default to a relatively low resolution for output, resulting in a fuzzy-looking result. Use the same resolutions you would for originals, and don't choose images made at screen resolution for a document you intend to print.

Does PDF to JPG lose quality?

When you save a PDF file as a JPG file, there would be a certain loss of quality. That is mainly because text and images stored in PDF files are saved in vector format. On the other hand, images in JPG files are saved in raster format.


1 Answers

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.

like image 110
roman Avatar answered Oct 08 '22 01:10

roman