Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to convert the HEIC image to Jpg using et PHImageManager

Ganesh.4229Ganesh.4229 USMember ✭✭

Hi All,

I found the below code to convert HEIC to Jpg in swift.
But I am trying to convert it in Xamarin, So some one could help me and suggest how to do the same in Xamarin.iOS or Forms

    let imageManager = PHImageManager.default()
    var photo : PHAsset
    var options : PHImageRequestOptions

    imageManager.requestImageData(for: photo, options: options, resultHandler: {
                    imageData,dataUTI,orientation,info in
    let ciImage = CIImage(data: imageData!)
    if #available(iOS 10.0, *) {
        data = CIContext().jpegRepresentation(of: ciImage!, colorSpace: CGColorSpaceCreateDeviceRGB())!
        // upload image data

Thank you,

Best Answer


  • LandLuLandLu Member, Xamarin Team Xamurai

    Try the code below:

    PHImageManager.DefaultManager.RequestImageData(phAsset, null, (data, dataUti, orientation, info) =>
        CIImage ciImage = new CIImage(data);
        var defaultContext = CIContext.Create();
        CGImage outputImage = defaultContext.CreateCGImage(ciImage, ciImage.Extent, CIFormat.ARGB8, CGColorSpace.CreateDeviceRGB());
        // Or use this:
        // CGImage outputImage = defaultContext.CreateCGImage(ciImage, ciImage.Extent);
        var image = new UIImage(outputImage);
        var outputData = image.AsJPEG();
  • Ganesh.4229Ganesh.4229 USMember ✭✭
    edited January 2019

    Thanks for the reply, it seems phAsset is a compilation error.
    How do I create phAsset using with file path or Filedata?

    I have file data which is picked from the Files using File picker.

        FileData fileData = await CrossFilePicker.Current.PickFile();
                if (fileData == null)
                string fileName = fileData.FileName;
        string filePath = fileData.FilePath;

    Now I want to convert this file /fileData to jpg. So how do I pass this fil path/data for above PHImageManager code

    Thank you,

  • Ganesh.4229Ganesh.4229 USMember ✭✭

    Thank you, If pick the photos gallery working fine. Thank you.

  • AbhayLAbhayL Member ✭✭

    I'm using same approach... and trying to return image's base64 to save in DB.

    However my image gets cropped.

    Could you please help @LandLu

Sign In or Register to comment.