メインメニューを開く

差分

Xamarin.Mac/CGImageをファイルに保存する

1,396 バイト追加, 2015年10月1日 (木) 02:03
ページの作成:「==実装1== Xamarin.Mac 2.0系の場合はCGImageDestination.Createのまま。 Xamarin.Mac 1.0系の場合はCGImageDestination.FromUrlに変更する。 <source lang="c...」
==実装1==
Xamarin.Mac 2.0系の場合はCGImageDestination.Createのまま。
Xamarin.Mac 1.0系の場合はCGImageDestination.FromUrlに変更する。
<source lang="csharp">
using System;
using Foundation;
using CoreGraphics;
using ImageIO;

namespace Library
{
public static class CGImageExtensions
{
public static bool Save(this CGImage cgImage, string path, string typeIdentifier)
{
using (var url = NSUrl.FromFilename(path))
using (var dst = CGImageDestination.Create(url, typeIdentifier, imageCount:1))
{
if (dst == null)
{
return false;
}

dst.AddImage(cgImage);

return dst.Close();
}
}
}
}
</source>

引数typeIdentifierはMobileCoreServices.UTTypeに定義されている定数文字列群を指定する。
拡張子で自動判別するようにしてMobileCoreServices.UTTypeを無視するようにした方が直感的かもしれない。
<source lang="csharp">
cgImage.Save(save, MobileCoreServices.UTType.PNG);
</source>

==関連項目==
*[[Xamarin.Mac/NSViewの描画内容をファイルに保存する]]
*[[Xamarin.Mac/CoreGraphicsの基本的な使い方]]
*[[Xamarin.Mac/NSOpenGLViewを使う]]
*[[Xamarin.Mac/効果音を再生する]]

==参考文献==
{{reflist}}

{{stub}}
匿名利用者