【问题标题】:Cannot use GPUImage to scale UIImage on iOS7无法在 iOS7 上使用 GPUImage 缩放 UIImage
【发布时间】:2015-02-07 02:15:11
【问题描述】:

我已经编写了一年多的代码,可以成功缩放、旋转 UIImage 并将其转换为 1bpp(黑白)。它在我的 iPhone 5s、我测试过的 iPad 等上运行良好。但是,我运行 iOS 7.1.2 的 iPhone 4 崩溃了。

我的代码(UIImage 上的一个类别)是:

- (void) prepareImageForOCRWithCompletionHandler:(void (^) (UIImage *image))completion {

    if (!completion) return; // no sense in doing anything if nobody listens for the completion

    int width = self.size.width;
    int height = self.size.height;

    DLog(@"width=%d, height=%d", width, height);

    if (width > height) {
        int temp = width;
        width = height;
        height = temp;
    }

    int scaledWidth = 1728; // class F tiffs should be 1728, 2048 or 2483 pixels wide http://www.libtiff.org/support.html
    int scaledHeight = scaledWidth * height / width;

    DLog(@"width=%d, height=%d, scaledWidth=%d, scaledHeight=%d", width, height, scaledWidth, scaledHeight);

    GPUImagePicture *source = [[GPUImagePicture alloc] initWithImage:self];

    GPUImageFilter *rotateFilter = [[GPUImageFilter alloc] init];
    [rotateFilter setInputRotation:kGPUImageRotateRight atIndex:0];

    GPUImageTransformFilter *scaleFilter = [[GPUImageTransformFilter alloc] init];
    [scaleFilter forceProcessingAtSizeRespectingAspectRatio:CGSizeMake(scaledWidth, scaledHeight)];

    GPUImageAdaptiveThresholdFilter *thresholdFilter = [[GPUImageAdaptiveThresholdFilter alloc] init];

    [source addTarget:rotateFilter];
    [rotateFilter addTarget:scaleFilter];
    [scaleFilter addTarget:thresholdFilter];

    [source processImageWithCompletionHandler:^{
        UIImage *result = [thresholdFilter imageFromCurrentlyProcessedOutput];
        completion(result);
    }];
}

在 Xcode 控制台中使用此消息调用 GPUImage 完成处理程序之前应用程序崩溃:

2015-02-05 13:39:10.131 MyApp[345:60b] -[UIImage(BJM) prepareImageForOCRWithCompletionHandler:] [Line 54] width=1936, height=2592
2015-02-05 13:39:10.133 MyApp[345:60b] -[UIImage(BJM) prepareImageForOCRWithCompletionHandler:] [Line 65] width=1936, height=2592, scaledWidth=1728, scaledHeight=2313
2015-02-05 13:39:12.328 MyApp[345:7273] *** Assertion failure in -[GPUImageTransformFilter createFilterFBOofSize:], /Users/brian/repos/GPUImage/framework/Source/GPUImageFilter.m:380
2015-02-05 13:39:12.364 MyApp[345:7273] void uncaughtExceptionHandler(NSException *__strong) [Line 75] uncaught exception: Incomplete filter FBO: 36054
2015-02-05 13:39:12.366 MyApp[345:7273] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Incomplete filter FBO: 36054'
*** First throw call stack:
(0x30560f83 0x3ad11ccf 0x30560e5d 0x30f0ed5b 0x2a7adf 0x2aa82d 0x2a73a7 0x2a7c77 0x2a7daf 0x2b0265 0x2a80d5 0x2a9285 0x2acd49 0x3b1f9833 0x3b1f9ded 0x3b1fa297 0x3b20c88d 0x3b20cb21 0x3b33bbd3 0x3b33ba98)
libc++abi.dylib: terminating with uncaught exception of type NSException

如果我从管道中删除 GPUImageTransformFilter,它就可以工作。如果我恢复转换过滤器但将 1728 更改为 1000,它也可以工作。这似乎是原始图像太小的问题。但是,原件是 1936x2592。我还将我的宽度和高度整数更改为 CGFloats 认为舍入错误导致了问题。没有运气。

这确实是一个令人羞愧的问题。感谢您的宝贵时间。

【问题讨论】:

    标签: ios objective-c uiimage gpuimage


    【解决方案1】:

    您的问题与图像大小有关。在错误读数中,我看到您在图像中使用了 2592 的高度。

    对于 iPhone 4 及更早的设备,其 GPU 在任一维度上的纹理大小限制为 2048。尝试创建大于宽度或高度的纹理将导致失败,我对此提出断言。

    很遗憾,我目前不支持大于您正在使用的任何 GPU 的纹理限制的图像。您需要找到另一种方法来调整这些旧设备上大于 2048x2048 的图像的大小。我在框架内的少数几个地方执行此操作(例如,处理在 iPhone 4 上拍摄的照片),但不是所有地方。

    【讨论】:

    • 哇,谢谢。有没有办法向 GPU 询问其最大纹理大小?如果我提前知道这一点,我可以切换到 CoreGraphics 调整大小例程。它不会那么快,但至少它会起作用。我看到在 OpenGL ES Programming Guide 中提到了这些限制,但链接已失效。
    • 没关系,我想我找到了。也谢谢你。 stackoverflow.com/a/18047910/300986
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    相关资源
    最近更新 更多