问题
I'm using NSSavePanel to let user select image to save to in my app. Everything worked fine until I enabled app sandboxing and entitlements. The problem occurs with selection of an already existing file. My code is like this:
// Create a URL to our file destination and a CGImageDestination to save to. CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((CFURLRef)[savePanel URL], (CFStringRef)newUTType, 1, NULL); CGImageDestinationAddImage(imageDestination, cgimage, (CFDictionaryRef)metaData); const bool result = CGImageDestinationFinalize(imageDestination);
It works when selecting new file to save the image, but when I select existing file it creates strange named file besides existing file and fails to overwrite the contents of destination url. And even worse, I get no error in return and cannot detect the failure. Is this a bug in CoreGraphics or in my code? Is there any workaround for this issue?
回答1:
Finally I have discovered combination of core graphics calls to overwrite an already existing image working in sandboxed environment: CGDataConsumerCreateWithURL
followed by CGImageDestinationCreateWithDataConsumer
. So it seems CGImageDestinationCreateWithURL
is broken (at least in OS X Lion 10.7.1
) with sandbox enabled.
来源:https://stackoverflow.com/questions/7511696/nssavepanel-cgimagedestinationfinalize-and-os-x-sandbox