ZXing无法写入png输出



我从未经历过这个问题,我能够在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);
}

最终找到了问题的来源,事实证明这是因为应用程序是沙盒。关闭沙盒解决了问题。

要使用沙箱进行此操作,解决方案是放置一个保存面板,如果返回正常,则将文件保存到保存面板的URL。

最新更新