after cropping the image quality changes, why?

I have a cropping project in xamarin forms for ios , after getting the image the quality of the image decrease, could anyone see my code in the github link below and ell me what have done wrong? thnx.

https://github.com/SoniaCH/CroppingIOS

Answers

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    If you crop and expand the cropped image to be the same size as the uncropped image, quality will decrease as the PPI (pixels per inch) will be lower.

    Also, it would help if instead of just linking to the repo, you link to the specific code for the cropping so someone trying to help does not have to search for the relevant cropping code. Even better, paste the relevant code directly in your question.

    I did not look at your cropping code yet, but my answer above is correct without even needing to see any code. This is true no matter how you crop an image... you are cutting off pixels from the sides of the image. So lets ay you have a 100 x 100 image being displayed in a 1" by 1" space. You have a PPI of 100. If you then crop the image and crop off 10 pixels form each side, you now have an 80x80 image, so if you display that in the same 1" x 1" space, your PPI is now 80, which would be lower quality.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Also... If you started with a JPG which is a compressed format... then saved as a JPG... you're compressing the already compressed image. Every compression is a reduction in quality acheived by averaging neighboring pixels. If you were to keep doing it... compress... compress again... compress again... even without cropping... Eventually if you did it enough times you'd be left with a bitmap of all one color.

  • SoniaDevRootSoniaDevRoot Member ✭✭
    edited February 14

    @ClintStLaurent and @JGoldberger thnx for your answer and it is correct but for my case it was because I was trying to get the image to fill in the screen so the quality was bad from the beginning , so I chnaged the whole method and I used the UIImagePickerController to do the editing, it's good but here after resizing the image there is an issue with moving it.
    here is the code that I used:

    ` {if (UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
    {
    var imagePicker = new UIImagePickerController { SourceType = UIImagePickerControllerSourceType.Camera };
    imagePicker.AllowsEditing = true;
    var window = UIApplication.SharedApplication.KeyWindow;
    var vc = window.RootViewController;
    while (vc.PresentedViewController != null)
    {
    vc = vc.PresentedViewController;
    }
    vc.PresentViewController(imagePicker, true, null);

                    imagePicker.FinishedPickingMedia += (sender, e) =>
                    {
                        UIImage image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerEditedImage"));
                        Data = null;
                        if (imageType == FileFormatEnum.PNG)
                            Data = image.AsPNG();
                        else
                            Data = image.AsJPEG();
                        byte[] myByteArray = new byte[Data.Length];
                        if (myByteArray.Length < 10000000)
                        {
                            DependencyFactory._ProductCreationViewModel.PhotoCrapped = myByteArray;
                            System.Runtime.InteropServices.Marshal.Copy(Data.Bytes, myByteArray, 0, Convert.ToInt32(Data.Length));
                        }
                        else
                        {
                            string result = "bad";
                            MessagingCenter.Send<string>(result, "ResultSelected");
                            DependencyFactory._ProductCreationViewModel._nav.PopAsync();
                        }
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            vc.DismissViewController(true, null);
                        });
                    };
                    imagePicker.Canceled += (sender, e) =>
                    {
                        vc.DismissViewController(true, null);
                        byte[] rst = new byte[0];
                        string result = "canceled";
                        MessagingCenter.Send<string>(result, "ResultSelected");
                    };
                }}`
    


Sign In or Register to comment.