ZXing 无法写入 png 输出

ZXing can't write png output

我从未遇到过该问题,3 个月前我还可以使用相同的代码。更新到 macOS High Sierra 后,我无法使用 ZXing 库 运行 此代码。不确定它是否已链接,但这是我拥有的:

错误:

IIOImageWriteSession:113: cannot create: '/Users/****/Desktop/test.PNG.sb-8ff9679f-SRYKGg'
     error = 1 (Operation not permitted)

代码:

NSError * error = nil;
ZXMultiFormatWriter * writer = [ZXMultiFormatWriter writer];
ZXBitMatrix* result = [writer encode:@"AM233X05987"
                              format:kBarcodeFormatDataMatrix
                               width:500
                              height:500
                               error:&error];

if (result) {
    CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];

    CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:@"/Users/****/Desktop/test.PNG"];

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);

    if (!destination) {
        NSLog(@"Failed to create CGImageDestination" );
        return NO;
    }

    CGImageDestinationAddImage(destination, image, nil);

    if (!CGImageDestinationFinalize(destination)) {
        NSLog(@"Failed to write image to /Users/alex/Desktop/Barcodes/");
        CFRelease(destination);
        return NO;
    }

    CFRelease(destination);

} else {
    NSString * errorMessage = [error localizedDescription];
    NSLog(@"%@", errorMessage);
}

终于找到问题的根源了,原来是因为App被沙盒化了。关闭沙盒解决了这个问题。

要在 Sandbox ON 的情况下实现这一点,一个解决方案是建立一个保存面板,如果 returns OK,将文件保存到保存面板的 URL。