private static void SaveTempImage(CIImage fullimage, UIImage image, string outputFilename, int quality, bool rotate) { var imageData = image.AsJPEG(quality); var dataProvider = new CGDataProvider(imageData); var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default); var imageWithExif = new NSMutableData(); var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1); var cgImageMetadata = new CGMutableImageMetadata(); var options = new CGImageDestinationOptions(); if (fullimage.Properties.DPIWidthF != null) { options.Dictionary[ImageIO.CGImageProperties.DPIWidth] = new NSNumber((nfloat)fullimage.Properties.DPIWidthF); } if (fullimage.Properties.DPIWidthF != null) { options.Dictionary[ImageIO.CGImageProperties.DPIHeight] = new NSNumber((nfloat)fullimage.Properties.DPIHeightF); } options.ExifDictionary = fullimage.Properties?.Exif ?? new CGImagePropertiesExif(); options.TiffDictionary = fullimage.Properties?.Tiff ?? new CGImagePropertiesTiff(); options.GpsDictionary = fullimage.Properties?.Gps ?? new CGImagePropertiesGps(); options.JfifDictionary = fullimage.Properties?.Jfif ?? new CGImagePropertiesJfif(); options.IptcDictionary = fullimage.Properties?.Iptc ?? new CGImagePropertiesIptc(); if (rotate) { options.Dictionary[ImageIO.CGImageProperties.Orientation] = new NSString(UIImageOrientation.Up.ToString()); var tiffDict = new CGImagePropertiesTiff(); foreach (KeyValuePair <NSObject, NSObject> x in options.TiffDictionary.Dictionary) { tiffDict.Dictionary.SetValueForKey(x.Value, x.Key as NSString); } tiffDict.Dictionary.SetValueForKey(new NSNumber(1), new NSString("Orientation")); options.TiffDictionary = tiffDict; } else { if (fullimage.Properties.Orientation != null) { options.Dictionary[ImageIO.CGImageProperties.Orientation] = new NSString(image.Orientation.ToString()); } } destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, options); var success = destination.Close(); if (success) { imageWithExif.Save(outputFilename, true); } else { imageData.Save(outputFilename, true); } }
protected override Task <PImage> GenerateImageFromDecoderContainerAsync(IDecodedImage <PImage> decoded, ImageInformation imageInformation, bool isPlaceholder) { PImage result; if (decoded.IsAnimated) { #if __IOS__ result = PImage.CreateAnimatedImage(decoded.AnimatedImages .Select(v => v.Image) .Where(v => v?.CGImage != null).ToArray(), decoded.AnimatedImages.Sum(v => v.Delay) / 100.0); #elif __MACOS__ using (var mutableData = NSMutableData.Create()) { var fileOptions = new CGImageDestinationOptions { GifDictionary = new NSMutableDictionary() }; fileOptions.GifDictionary[ImageIO.CGImageProperties.GIFLoopCount] = new NSString("0"); //options.GifDictionary[ImageIO.CGImageProperties.GIFHasGlobalColorMap] = new NSString("true"); using (var destintation = CGImageDestination.Create(mutableData, MobileCoreServices.UTType.GIF, decoded.AnimatedImages.Length, fileOptions)) { for (var i = 0; i < decoded.AnimatedImages.Length; i++) { var options = new CGImageDestinationOptions { GifDictionary = new NSMutableDictionary() }; options.GifDictionary[ImageIO.CGImageProperties.GIFUnclampedDelayTime] = new NSString(decoded.AnimatedImages[i].Delay.ToString()); destintation.AddImage(decoded.AnimatedImages[i].Image.CGImage, options); } destintation.Close(); } result = new PImage(mutableData); // TODO I really don't know why representations count is 1, anyone? // var test = result.Representations(); } #endif } else { result = decoded.Image; } return(Task.FromResult(result)); }
public string CreateGif(string frame1Path, string frame2Path, string frame3Path, string frame4Path, string webId, string path = "") { List <UIImage> listOfFrame = new List <UIImage>(); UIImage image1 = new UIImage(frame1Path); listOfFrame.Add(image1); UIImage image2 = new UIImage(frame2Path); listOfFrame.Add(image2); UIImage image3 = new UIImage(frame3Path); listOfFrame.Add(image3); UIImage image4 = new UIImage(frame4Path); listOfFrame.Add(image4); NSMutableDictionary fileProperties = new NSMutableDictionary { { CGImageProperties.GIFLoopCount, new NSNumber(1) } }; NSUrl documentsDirectoryUrl = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User, null, true, out _); NSUrl fileUrl = documentsDirectoryUrl.Append(webId + ".gif", false); var destination = CGImageDestination.Create(fileUrl, MobileCoreServices.UTType.GIF, 4); destination.SetProperties(fileProperties); foreach (var frame in listOfFrame) { //difference is here, i create a var option and i set the //GifDictionary var options = new CGImageDestinationOptions(); options.GifDictionary = new NSMutableDictionary(); options.GifDictionary[CGImageProperties.GIFDelayTime] = new NSNumber(1f); var cgImage = frame.CGImage; if (cgImage != null) { destination.AddImage(cgImage, options); } } if (!destination.Close()) { Console.WriteLine("Failed to finalize the image destination"); } return(fileUrl.Path); }
private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path) { var imageData = image.AsJPEG(quality); var dataProvider = new CGDataProvider(imageData); var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default); var imageWithExif = new NSMutableData(); var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1); var cgImageMetadata = new CGMutableImageMetadata(); var destinationOptions = new CGImageDestinationOptions(); if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary)) { destinationOptions.ExifDictionary = new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary)) { destinationOptions.TiffDictionary = new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary)) { destinationOptions.GpsDictionary = new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary)) { destinationOptions.JfifDictionary = new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary)) { destinationOptions.IptcDictionary = new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary); } destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions); var success = destination.Close(); if (success) { imageWithExif.Save(path, true); } return(success); }
protected override Task <PImage> GenerateImageFromDecoderContainerAsync(IDecodedImage <PImage> decoded, ImageInformation imageInformation, bool isPlaceholder) { PImage result; if (decoded.IsAnimated) { #if __IOS__ result = PImage.CreateAnimatedImage(decoded.AnimatedImages .Select(v => v.Image) .Where(v => v?.CGImage != null).ToArray(), decoded.AnimatedImages.Sum(v => v.Delay) / 100.0); #elif __MACOS__ using (var mutableData = NSMutableData.Create()) { using (var destintation = CGImageDestination.Create(mutableData, MobileCoreServices.UTType.GIF, decoded.AnimatedImages.Length)) { for (int i = 0; i < decoded.AnimatedImages.Length; i++) { var options = new CGImageDestinationOptions(); options.GifDictionary = new NSMutableDictionary(); options.GifDictionary[ImageIO.CGImageProperties.GIFDelayTime] = NSNumber.FromDouble(decoded.AnimatedImages[i].Delay / 100.0d); destintation.AddImage(decoded.AnimatedImages[i].Image.CGImage, options); } destintation.Close(); } // TODO I really don't know why it's not working. Anyone? result = new PImage(mutableData); } #endif } else { result = decoded.Image; } return(Task.FromResult(result)); }
private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path) { try { var finalQuality = quality; var imageData = image.AsJPEG(finalQuality); //continue to move down quality , rare instances while (imageData == null && finalQuality > 0) { finalQuality -= 0.05f; imageData = image.AsJPEG(finalQuality); } if (imageData == null) { throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level"); } var dataProvider = new CGDataProvider(imageData); var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default); var imageWithExif = new NSMutableData(); var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1); var cgImageMetadata = new CGMutableImageMetadata(); var destinationOptions = new CGImageDestinationOptions(); if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation)) { destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = meta[ImageIO.CGImageProperties.Orientation]; } if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth)) { destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] = meta[ImageIO.CGImageProperties.DPIWidth]; } if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight)) { destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] = meta[ImageIO.CGImageProperties.DPIHeight]; } if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary)) { destinationOptions.ExifDictionary = new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary)) { destinationOptions.TiffDictionary = new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary)) { destinationOptions.GpsDictionary = new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary)) { destinationOptions.JfifDictionary = new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary)) { destinationOptions.IptcDictionary = new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary); } destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions); var success = destination.Close(); if (success) { imageWithExif.Save(path, true); } return(success); } catch (Exception ex) { Console.WriteLine($"Unable to save image with metadata: {ex}"); } return(false); }
internal static bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path, string pathExtension) { if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) { return(SaveImageWithMetadataiOS13(image, quality, meta, path, pathExtension)); } try { pathExtension = pathExtension.ToLowerInvariant(); var finalQuality = quality; var imageData = pathExtension == "jpg" ? image.AsJPEG(finalQuality) : image.AsPNG(); //continue to move down quality , rare instances while (imageData == null && finalQuality > 0) { finalQuality -= 0.05f; imageData = image.AsJPEG(finalQuality); } if (imageData == null) { throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level"); } var dataProvider = new CGDataProvider(imageData); var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default); var imageWithExif = new NSMutableData(); var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1); var cgImageMetadata = new CGMutableImageMetadata(); var destinationOptions = new CGImageDestinationOptions(); if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation)) { destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = meta[ImageIO.CGImageProperties.Orientation]; } if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth)) { destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] = meta[ImageIO.CGImageProperties.DPIWidth]; } if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight)) { destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] = meta[ImageIO.CGImageProperties.DPIHeight]; } if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary)) { destinationOptions.ExifDictionary = new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary)) { var existingTiffDict = meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary; if (existingTiffDict != null) { var newTiffDict = new NSMutableDictionary(); newTiffDict.SetValuesForKeysWithDictionary(existingTiffDict); newTiffDict.SetValueForKey(meta[ImageIO.CGImageProperties.Orientation], ImageIO.CGImageProperties.TIFFOrientation); destinationOptions.TiffDictionary = new CGImagePropertiesTiff(newTiffDict); } } if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary)) { destinationOptions.GpsDictionary = new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary)) { destinationOptions.JfifDictionary = new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary)) { destinationOptions.IptcDictionary = new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary); } destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions); var success = destination.Close(); if (success) { var saved = imageWithExif.Save(path, true, out NSError error); if (error != null) { Debug.WriteLine($"Unable to save exif data: {error.ToString()}"); } imageWithExif.Dispose(); imageWithExif = null; } return(success); } catch (Exception ex) { Console.WriteLine($"Unable to save image with metadata: {ex}"); } return(false); }
private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary meta, string path) { try { var imageData = GetImageWithQuality(quality, image); var dataProvider = new CGDataProvider(imageData); var cgImageFromJpeg = CGImage.FromJPEG(dataProvider, null, false, CGColorRenderingIntent.Default); var imageWithExif = new NSMutableData(); var destination = CGImageDestination.Create(imageWithExif, UTType.JPEG, 1); var cgImageMetadata = new CGMutableImageMetadata(); var destinationOptions = new CGImageDestinationOptions(); if (meta.ContainsKey(ImageIO.CGImageProperties.DPIWidth)) destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIWidth] = meta[ImageIO.CGImageProperties.DPIWidth]; if (meta.ContainsKey(ImageIO.CGImageProperties.DPIHeight)) destinationOptions.Dictionary[ImageIO.CGImageProperties.DPIHeight] = meta[ImageIO.CGImageProperties.DPIHeight]; if (meta.ContainsKey(ImageIO.CGImageProperties.ExifDictionary)) { destinationOptions.ExifDictionary = new CGImagePropertiesExif(meta[ImageIO.CGImageProperties.ExifDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.TIFFDictionary)) { destinationOptions.TiffDictionary = new CGImagePropertiesTiff(meta[ImageIO.CGImageProperties.TIFFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.GPSDictionary)) { destinationOptions.GpsDictionary = new CGImagePropertiesGps(meta[ImageIO.CGImageProperties.GPSDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.JFIFDictionary)) { destinationOptions.JfifDictionary = new CGImagePropertiesJfif(meta[ImageIO.CGImageProperties.JFIFDictionary] as NSDictionary); } if (meta.ContainsKey(ImageIO.CGImageProperties.IPTCDictionary)) { destinationOptions.IptcDictionary = new CGImagePropertiesIptc(meta[ImageIO.CGImageProperties.IPTCDictionary] as NSDictionary); } if (options.RotateImage) { if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation)) destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = new NSString(UIImageOrientation.Up.ToString()); destinationOptions.TiffDictionary.Orientation = CoreImage.CIImageOrientation.TopLeft; } else { if (meta.ContainsKey(ImageIO.CGImageProperties.Orientation)) destinationOptions.Dictionary[ImageIO.CGImageProperties.Orientation] = meta[ImageIO.CGImageProperties.Orientation]; } destination.AddImageAndMetadata(cgImageFromJpeg, cgImageMetadata, destinationOptions); var success = destination.Close(); if (success) { imageWithExif.Save(path, true); } return success; } catch (Exception ex) { Console.WriteLine($"Unable to save image with metadata: {ex}"); } return false; }