private void FreeBitmap() { dataProvider?.Dispose(); dataProvider = null; bitmapData?.Dispose(); bitmapData = null; }
public void SaveTo(Stream stream, CompressionFormat compression) { var data = new NSMutableData(); CGImageDestination dest = null; switch (compression) { case CompressionFormat.Jpeg: dest = CGImageDestination.Create(data, MobileCoreServices.UTType.JPEG, imageCount: 1); break; case CompressionFormat.Png: dest = CGImageDestination.Create(data, MobileCoreServices.UTType.PNG, imageCount: 1); break; } if (dest != null) { dest.AddImage(cgImage); dest.Close(); using (var bitmapStream = data.AsStream()) { bitmapStream.CopyTo(stream); } data.Dispose(); } }
public void RedrawDailyStockPDF(bool DocumentSigned) { // render created preview in PDF context NSMutableData pdfData = new NSMutableData(); UIGraphics.BeginPDFContext(pdfData, GeneratedPDFView.Bounds, null); UIGraphics.BeginPDFPage(); GeneratedPDFView.Layer.RenderInContext(UIGraphics.GetCurrentContext()); UIGraphics.EndPDFContent(); // save the rendered context to disk NSError err; string pdfID = String.Format("{0}_{1}_{2}_UsedStock", MyConstants.EmployeeID, MyConstants.EmployeeName, DateTime.Now.Date.ToString("yyyy-MM-dd")); string pdfFileName; if (DocumentSigned) { pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Signed.pdf"); } else { pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Not_Signed.pdf"); } pdfData.Save(pdfFileName, true, out err); if (err != null) { err.Dispose(); err = null; } if (pdfData != null) { pdfData.Dispose(); pdfData = null; } // set up UIWebView in signature view controller this.PDFView.MultipleTouchEnabled = true; this.PDFView.ScalesPageToFit = true; this.PdfFileName = pdfFileName; if (DocumentSigned) { PDFView.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(PdfFileName))); } }
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); }
public override void FinishedLoading(NSUrlConnection connection) { _imageSetter(imageData); imageData.Dispose(); imageData = null; }