macos — Создание CGImageDestinationRef с CGImageDestinationCreateWithURL в C ++ возвращает NULL

Я пытаюсь сделать снимок экрана для каждого монитора моей установки macOS 10.13 в C ++, используя методы, доступные в некоторых платформах OSX, но используя CGImageDestinationCreateWithURL создать CGImageDestinationRef пункт назначения возвращается NULL и я понятия не имею, что я делаю не так.

Проблема, которую я думаю, у меня заключается в строке:

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

Код, который я использую, следующий:

main.cpp:

#include <iostream>
#include <string>
#include <QuartzCore/QuartzCore.h>
#include <CoreServices/CoreServices.h>
#include <ImageIO/ImageIO.h>

int main(int argc, const char * argv[]) {
std::string baseImageOutput = "/Users/bogdan/Desktop";
std::string pathSeparator = "/";
std::string baseImageName = "image-";
std::string imageExtension = ".png";

CGDisplayCount displayCount;
CGDirectDisplayID displays[32];

// grab the active displays
CGGetActiveDisplayList(32, displays, &displayCount);

// go through the list
for (int i = 0; i < displayCount; i++) {
std::string imagePath = baseImageOutput + pathSeparator + baseImageName + std::to_string(i) + imageExtension;
const char *charPath = imagePath.c_str();

CFStringRef imageOutputPath = CFStringCreateWithCString(kCFAllocatorDefault, charPath, kCFURLPOSIXPathStyle);
// make a snapshot of the current display
CGImageRef image = CGDisplayCreateImage(displays[i]);

CFURLRef url = CFURLCreateWithString(kCFAllocatorDefault, imageOutputPath, NULL);

// The following CGImageDestinationRef variable is NULL
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);

if (!destination) {
std::cout<< "The destination does not exist: " << imagePath << std::endl;
CGImageRelease(image);
return 1;
}

CGImageDestinationAddImage(destination, image, NULL);

if (!CGImageDestinationFinalize(destination)) {
std::cout << "Failed to write image to the path" << std::endl;;
CFRelease(destination);
CGImageRelease(image);
return 1;
}

CFRelease(destination);
CGImageRelease(image);
}

std::cout << "It Worked. Check your desktop" << std::endl;;
return 0;
}

Я правильно создаю пункт назначения?

1

Решение

Нашел решение.

Кажется, что baseImageOutput должен предшествовать file:// так что окончательный URL-адрес действителен, поэтому мы имеем

std::string baseImageOutput = "file:///Users/bogdan/Desktop";

Вместо

std::string baseImageOutput = "/Users/bogdan/Desktop";
2

Другие решения

Других решений пока нет …