如何使用 CGBitmapContextCreateImage 获取优质图像

发布于 2024-12-17 13:15:10 字数 1576 浏览 5 评论 0原文

如何?... 我尝试使用 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;
}

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

How?...
I am tring to get High Quality image using CGBitmapContextCreateImage() and tried to do it with the following code.

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

But, i couldn't get the blur image, so there is some difference between the rendered image and original screen image.
I can't know the reason.
Please help me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文