Ejemplo n.º 1
0
        static bool SaveImageWithMetadataiOS13(UIImage image, NSDictionary meta, string path, string fileExt)
        {
            try
            {
                var imageData = fileExt == "png" ? image.AsPNG() : image.AsJPEG();
                if (imageData == null)
                {
                    throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level");
                }

                // Copy over meta data
                using (var ciImage = CIImage.FromData(imageData))
                    using (var newImageSource = ciImage.CreateBySettingProperties(meta))
                        using (var ciContext = new CIContext())
                        {
                            if (fileExt == "png")
                            {
                                return(ciContext.WritePngRepresentation(newImageSource, NSUrl.FromFilename(path), CIFormat.ARGB8, CGColorSpace.CreateSrgb(), new NSDictionary(), out var error2));
                            }

                            return(ciContext.WriteJpegRepresentation(newImageSource, NSUrl.FromFilename(path), CGColorSpace.CreateSrgb(), new NSDictionary(), out var error));
                        }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unable to save image with metadata: {ex}");
            }
            return(false);
        }
Ejemplo n.º 2
0
		internal static bool SaveImageWithMetadataiOS13(UIImage image, float quality, NSDictionary meta, string path, string pathExtension)
		{
			try
			{
				pathExtension = pathExtension.ToLowerInvariant();
				var finalQuality = quality;
				var imageData = pathExtension == "png" ? image.AsPNG(): 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");

				// Copy over meta data
				using var ciImage = CIImage.FromData(imageData);
				using var newImageSource = ciImage.CreateBySettingProperties(meta);
				using var ciContext = new CIContext();

				if (pathExtension == "png")
					return ciContext.WritePngRepresentation(newImageSource, NSUrl.FromFilename(path), CIFormat.ARGB8, CGColorSpace.CreateSrgb(), new NSDictionary(), out var error2);
				
				return ciContext.WriteJpegRepresentation(newImageSource, NSUrl.FromFilename(path), CGColorSpace.CreateSrgb(), new NSDictionary(), out var error);
			}
			catch (Exception ex)
			{
				Console.WriteLine($"Unable to save image with metadata: {ex}");
			}

			return false;
		}