我从未遇到过这个问题,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);
}
答案 0 :(得分:0)
终于找到了问题的根源,事实证明这是因为App是Sandboxed。关闭Sandbox解决了这个问题。
要通过Sandbox ON获得此功能,解决方案是设置“保存”面板,如果返回“确定”,则将文件保存到“保存”面板的URL。