1

如何?...我正在尝试使用 CGBitmapContextCreateImage() 获取高质量图像,并尝试使用以下代码来实现。

- (CGImageRef) imageForPageIndex:(NSUInteger)pageIndex {

    NSUInteger width = pageSize.width;
    NSUInteger height = pageSize.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    NSUInteger bytesPerPixel = 8;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;

        void* imageData = malloc(width * height * bytesPerPixel);

    CGContextRef context = CGBitmapContextCreate(imageData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, pageSize.width * 2, pageSize.height * 2, 8, pageSize.width * 8, colorSpace, kCGImageAlphaPremultipliedLast | CGBitmapByteOrder32Big);

    CGColorSpaceRelease(colorSpace);
    CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width, pageSize.height));

    CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width, pageSize.height));
    CGAffineTransform flipVertical    = CGAffineTransformMake(1, 0, 0, -1, 0, pageSize.height);
    CGContextConcatCTM(context, flipVertical);

    [dataSource renderPageAtIndex:pageIndex inContext:context];

    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);


    [UIImage imageWithCGImage:image];
    CGImageRelease(image);

    return image;
}

但是,我无法获得模糊图像,因此渲染图像和原始屏幕图像之间存在一些差异。我不知道原因。请帮我...

4

0 回答 0