NSImage к Base64

ранее связанное решение - ужасный кусок кода; почти каждая строка содержит ошибку. Вместо этого используйте fgetcsv :

\n\n";
$f = fopen("so-csv.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "";
        foreach ($line as $cell) {
                echo "";
        }
        echo "\n";
}
fclose($f);
echo "\n
" . htmlspecialchars($cell) . "
";

6
задан Peter Hosey 3 June 2009 в 06:30
поделиться

3 ответа

An NSImage is a very abstract object. NSImage doesn't really care whether it's a raster image or a vector image; an NSImage object can even have raster, vector, and even programmatic representations all at once—it's that general.

Before you can generate Base64 data, you must decide what you want to encode.

The first step is to decide whether you want to encode a raster or vectors. The former is quite easy, and I'd guess it's probably what you meant. However, an NSImage could have come from a vector source, such as a PDF document.

If you know you created the image from a raster source, you can just encode that source data.

If it came from a vector source, you can still just encode that if you know the application on the decoding end will be able to handle it (e.g., if it's another Cocoa or Cocoa Touch app). On the other hand, if the app on the decoding end may be unable to handle vector data, then you should avoid this tactic.

The one solution that works in all cases is to use NSBitmapImageRep to create a raster capture of the image. Lock focus on the image, then create an NSBitmapImageRep using that method, then unlock focus. Then, use representationUsingType:properties: to generate PNG (or whatever format is appropriate) data for the image. Then Base64-encode the PNG (or whatever format) data.

4
ответ дан 9 December 2019 в 22:39
поделиться

У меня есть куча кода, включая анализ Base64 на основе реализации в OmniFoundation, в моем наборе инструментов на github . В частности, посмотрите Extensions / NSData + Base64.h.

2
ответ дан 9 December 2019 в 22:39
поделиться

Apple не предоставляет здесь какой-либо особой помощи, так что вам придется решать сложность самостоятельно, так или иначе.

К счастью, есть ресурсы, позволяющие упростить эту задачу. Первый подход - это буквально делать кодирование и декодирование самостоятельно. В Google Toolbox для Mac есть хороший пример такого подхода, и вы можете просто использовать этот исходный файл как есть:

http://code.google.com/p/google-toolbox-for-mac/source /browse/trunk/Foundation/GTMBase64.m

Если вы строите только для Mac, где доступны библиотеки OpenSSH, вы можете воспользоваться преимуществами некоторых функций в этих библиотеках для кодирования и декодирования:

http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/

2
ответ дан 9 December 2019 в 22:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: