Skip to content

Instantly share code, notes, and snippets.

@addr010
Created June 15, 2019 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addr010/d805b81959a34299b230dd4bdd14477f to your computer and use it in GitHub Desktop.
Save addr010/d805b81959a34299b230dd4bdd14477f to your computer and use it in GitHub Desktop.
Create Jpeg data from CGImage in Xamarin C#
static NSData JpegData(this CGImage cgImage, float compressionQuality)
{
var data = NSMutableData.Create();
var idst = CGImageDestination.Create(data, "UTTypeJPEG", 1, null);
if (idst != null)
{
var props = NSDictionary.FromObjectsAndKeys(
new[] { NSNumber.FromFloat(compressionQuality) },
new[] { CGImageDestinationOptionsKeys.LossyCompressionQuality }
);
idst.AddImage(cgImage, props);
idst.Close();
return NSData.FromData(data);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment