保持图像质量,同时将高分辨率pdf绘制到iPhone?

时间:2011-12-28 06:24:36

标签: iphone objective-c

我在iPhone中遇到与pdf绘图有关的问题。当我在ipad中绘制pdf时它工作正常,但在iphone页面图像质量不好。该图像在灰色背景处包含黑点 任何人都可以帮我解决这个问题吗?

感谢。

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

1 个答案:

答案 0 :(得分:5)

只是一个疯狂的猜测,因为没有一些实际的截图很难说,但你不在渲染代码中使用CGContextSetInterpolationQuality。也许CGContextSetInterpolationQuality(your_context, kCGInterpolationHigh)解决了这个问题。