Search code examples
objective-czxing

ZXing can't write png output


I never experienced that issue, I was able to use the same code 3 month ago. After updating to macOS High Sierra, I can't run this code with the ZXing library. Not sure if it's linked but here what I have:

Error:

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

Code:

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

Solution

  • Finally found the origin of the problem, it turns out this is because the App is Sandboxed. Turning off Sandbox fixed the issue.

    To get this with Sandbox ON, a solution is to put up a Save panel and, if it returns OK, saving the file to the Save panel's URL.