Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

UIImage.AsJPEG() returns “Cannot cast from source type to destination type.”

$
0
0

Hello, hope you don't mind the cross posting:

http://stackoverflow.com/questions/22361205/uiimage-asjpeg-returns-cannot-cast-from-source-type-to-destination-type

I'm building an iOS App where I need to use the camera and save images from it into the App folder.

When I try to convert the image from the camera to a JPEG or PNG, I'm getting: "Cannot cast from source type to destination type."

Here is the code I'm using:

public class ImagePickerDelegate : UIImagePickerControllerDelegate
{
    public ImagePickerDelegate()
    {
    }

    public override void FinishedPickingImage(UIImagePickerController picker, UIImage image, NSDictionary editingInfo)
    {
        var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string jpgFilename = System.IO.Path.Combine(documentsDirectory, string.Format("{0}.jpg", Guid.NewGuid()));

        using (NSData imageData = image.AsJPEG(0.2f))
        {
            NSError err;
            if (!imageData.Save(jpgFilename, false, out err))
            {
                Console.WriteLine("Saving of file failed: " + err.Description);
            }
        }
    }
}

The error occurs when I call:

using (NSData imageData = image.AsJPEG(0.2f))

Any ideas?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>