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

Answers

  • 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 8

    @LandLu
    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)
                    return; 
    
                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.

Sign In or Register to comment.