21.7 C
New York
Saturday, September 7, 2024

ios – Add GPS to UIImage C# MAUI


Good afternoon I create and save a UIImage to the applying folder on ios. I’ve a photograph geolocated and want to save lots of the photograph with it, however I am unable to discover data wherever on how to do that. Has anybody encountered the same drawback?

my code for creating picture and saving:

public class ScreenshotService : IScreenshotService
{
    /// 
    /// Incoming body
    /// 
    public byte[] imageArray { get; set; }
    /// 
    /// Body width
    /// 
    public int width { get; set; }
    /// 
    /// Body peak
    /// 
    public int peak { get; set; }
    public bool serviceLock { get; set; }
    /// 
    /// File title for saving the screenshot
    /// 
    non-public string fileName = "";
    /// 
    /// Geotag
    /// 
    public Location location { get; set; }

    //---------------------------------------------------------

    /// 
    /// Create a photograph
    /// 
    /// 
    /// 
    public Job CaptureAsync(View view)
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            attempt
            {
                Permission();
                // Convert shade area
                var imageArgb = ConvertRgbaToArgb(imageArray, peak, width);
                // Create CGDataProvider from byte array
                CGDataProvider dataProvider = new CGDataProvider(imageArgb, 0, imageArgb.Size);
                // Create CGImage from CGDataProvider
                CGImage cgImage = new CGImage(width, peak, 8, 32, width * 4, CGColorSpace.CreateDeviceRGB(), CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Big, dataProvider, null, false, CGColorRenderingIntent.Default);
                // Create UIImage from CGImage
                UIImage imageBase = new UIImage(cgImage);
                // Create a renderer for this view
                var handler = view.Handler as IPlatformViewHandler;
                var nativeView = handler?.PlatformView as UIView;
                if (nativeView == null) return;
                // Calculate the scale of the view
                var measurement = nativeView.Bounds.Measurement;
                // Begin a brand new graphics context based mostly on bitmap
                UIGraphics.BeginImageContextWithOptions(measurement, false, 0);
                // Get the present graphics context
                var context = UIGraphics.GetCurrentContext();
                // Render the view's layer within the present graphics context
                nativeView.Layer.RenderInContext(context);
                // Get the UIImage illustration of the present graphics context
                var imageOverlay = UIGraphics.GetImageFromCurrentImageContext();
                // Finish the graphics context based mostly on bitmap
                UIGraphics.EndImageContext();
                // Begin a brand new graphics context based mostly on bitmap with the picture measurement
                UIGraphics.BeginImageContextWithOptions(imageBase.Measurement, false, 0.0f);
                // Draw the bottom picture on the brand new context
                imageBase.Draw(new CGRect(0, 0, imageBase.Measurement.Width, imageBase.Measurement.Top));
                // Draw the overlay picture on the brand new context
                imageOverlay.Draw(new CGRect(0, 0, imageBase.Measurement.Width, imageBase.Measurement.Top));
                // Get the UIImage picture from the present graphics context
                UIImage resultImage = UIGraphics.GetImageFromCurrentImageContext();
                // Finish the graphics context based mostly on bitmap
                UIGraphics.EndImageContext();
                // Arrange the output file title
                SetupOutputFileName();
                // Save the picture in JPEG format
                attempt
                {
                    resultImage.AsJPEG().Save(fileName, false, out _);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error saving picture: {ex.Message}");
                }
            }
            catch (Exception ex)
            {

            }
        });
        return null;
    }

    //---------------------------------------------------------

I attempted this technique, but it surely did not work. Within the line

vacation spot.AddImage(cgImageTest, metadata); 

offers an error that

Incorrect quantity or kinds of arguments
attempt
{
    var locationManager = new CLLocationManager();
    locationManager.RequestWhenInUseAuthorization();
    CLLocation currentLocation = locationManager.Location;
    if (currentLocation != null)
    {
        // Create EXIF metadata
        var metadata = new NSDictionary();
        var gpsMetadata = new NSDictionary();

        gpsMetadata[CGImageProperties.GPSLatitude] = NSNumber.FromDouble(currentLocation.Coordinate.Latitude);
        NSObject nSObject = new NSObject();
        gpsMetadata[CGImageProperties.GPSLatitudeRef] = new NSString(currentLocation.Coordinate.Latitude >= 0 ? "N" : "S");
        gpsMetadata[CGImageProperties.GPSLongitude] = NSNumber.FromDouble(currentLocation.Coordinate.Longitude);
        gpsMetadata[CGImageProperties.GPSLongitudeRef] = new NSString(currentLocation.Coordinate.Longitude >= 0 ? "E" : "W");

        metadata[CGImageProperties.GPSDictionary] = gpsMetadata;

        // Save the picture with metadata
        var imageData = resultImage.AsJPEG();

        CGImageSource supply = CGImageSource.FromData(imageData);
        CGImage cgImageTest = supply.CreateImage(0, null);
        var uti = CGImageSource.TypeIdentifiers[0];
        var destinationData = new NSMutableData();
        var choices = new CGImageDestinationOptions(metadata);
        utilizing (var vacation spot = CGImageDestination.Create(destinationData, uti, 1))
        {
            vacation spot.AddImage(cgImageTest, choices);
            vacation spot.Shut();
        }

        // Save the picture to disk
        NSError error;
        destinationData.Save(fileName, false, out error);

        if (error != null)
        {
            Console.WriteLine($"Error saving picture: {error.LocalizedDescription}");
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error saving picture: {ex.Message}");
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles