Overwrite image (Photo Asset) flushing original photo

Michal_JanMichal_Jan ✭✭Member ✭✭

Hi,
When changing asset using asset.RequestContentEditingInput(…) i end up with modified image, what is ok.
But, is there any option to specify that the asset gets overwritten completely, without preserving the orignal photo ?

I want to give the user option to select whether they want to keep orignal file, or overwrite completely. Original image takes up storage, and in many cases I believe users will not be intereseted in reverting back to the original.

Any way to specify this upon edit ? , or afterwards (with a kind of FlushOriginal / Commit / whatever… call).
I've found an oposite method (reverting back to original, flushing changes).
PHAssetChangeRequest.RevertAssetContentToOriginal().
I need oposite to this one.

Thanks in advance,
Michal

Tagged:

Answers

  • ColeXColeX Xamurai Member, Xamarin Team Xamurai

    Try to return false with PHContentEditingInputRequestOptions.CanHandleAdjustmentData , according official documentation

    If your block returns YES, Photos provides the original asset data for editing. Your app uses the adjustment data to alter, add to, or reapply previous edits. (For example, an adjustment data may describe filters applied to a photo. Your app reapplies those filters and allows the user to change filter parameters, add new filters, or remove filters.)

    If your block returns NO, Photos provides the most recent asset data—the rendered output of all previous edits—for editing.

  • Michal_JanMichal_Jan ✭✭ Member ✭✭

    Hi,
    I've tried with both true and false returned, and in both cases the original image is retained.
    I think that the boolean value retuned is more relevant for the application - whether it is or is not able to "understand" the change applied, if the literal identifying which application changed the photo matches the application's own one.

    Thanks,
    Best regards,
    Michal

  • ColeXColeX Xamurai Member, Xamarin Team Xamurai
    edited September 3

    Could you share a basic project so that i can test on my side ?

  • Michal_JanMichal_Jan ✭✭ Member ✭✭

    Hi,
    The code to update image, given Asset URL is the following.
    The url is acquired upon image opening - for test purposes any valid URL to a photo can be used.

    public Task<bool> SavePhotoAsync(byte[] processedImageEncodedAsJpg, string currentPhotoURL)
            {
                bool editFailed = false;
    
                TaskCompletionSource<bool> taskCompletionSourceLocal = new TaskCompletionSource<bool>();
                var task = new Task(() =>
                {
                    try
                    {
    
                        var nsUrl = new NSUrl(currentPhotoURL);
                        var assets = PHAsset.FetchAssets(new NSUrl[] { nsUrl }, null);
                        var asset = assets.FirstOrDefault() as PHAsset;
    
                        // Prepare the options to pass when requesting to edit the image.
                        var options = new PHContentEditingInputRequestOptions();
                        options.CanHandleAdjustmentData = (ad) => false;
    
                        asset.RequestContentEditingInput(options, (contentEditingInput, requestStatusInfo) =>
                        {
    
                            NSData nsData = NSData.FromArray(processedImageEncodedAsJpg);
    
                            var adjustmentData = new PHAdjustmentData(
                                "My_App_iOS_Filter",
                                "1.0",
                                NSData.FromString("My App", NSStringEncoding.UTF8)
                            );
    
                            var contentEditingOutput = new PHContentEditingOutput(contentEditingInput);
    
                            nsData.Save(contentEditingOutput.RenderedContentUrl, true);
                            contentEditingOutput.AdjustmentData = adjustmentData;
    
                            // Ask the shared PHPhotoLibrary to perform the changes.
                            PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
                            {
                                try
                                {
                                    var request = PHAssetChangeRequest.ChangeRequest(asset);
                                    request.ContentEditingOutput = contentEditingOutput;                               
                                }
                                catch (Exception ex)
                                {
                                    editFailed = true;
                                }
                            },
                            (success, error) =>
                            {
                                if (!success || editFailed)
                                {
                                    taskCompletionSourceLocal.SetResult(false);
                                }
                                else
                                {
                                    taskCompletionSourceLocal.SetResult(true);
                                }
                            });
                        });
    
                    }
                    catch (Exception ex)
                    {
                        taskCompletionSourceLocal.SetResult(false);
                    }
    
                });
    
                task.Start();
                return taskCompletionSourceLocal.Task;
    
            }
    

    Thanks,
    Best regards,
    Michal

Sign In or Register to comment.