Forum Xamarin.iOS

Announcement:

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.

removing duplicate phassets from an list in xamarin.ios

AmejackAmejack USMember ✭✭✭

removing duplicate phassets from an list

Tagged:

Answers

  • Gustavo_OliveiraGustavo_Oliveira USMember ✭✭✭

    I think we will need more context here to understand the issue and be able to help.

  • AmejackAmejack USMember ✭✭✭

    this is my code
    Using gmImagePicker i m getting duplicate image if i select
    [Export("actionSheet:clickedButtonAtIndex:")]
    public async void Clicked(UIActionSheet actionSheet, nint buttonIndex)
    {
    switch (buttonIndex)
    {

                case 0:
                    //imagePicker = new UIImagePickerController();
    
                    //if (UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
                    //{
                    //    imagePicker = new UIImagePickerController();
                    //    this.imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
                    //    imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.Camera);
                    //}
                    //else
                    //{
                    //    var alert = UIAlertController.Create("No Camera", "Sorry, this device has no camera", UIAlertControllerStyle.Alert);
                    //    alert.AddAction(UIAlertAction.Create(AppConstants.OK, UIAlertActionStyle.Cancel, null));
                    //    PresentViewController(alert, true, null);
                    //}
                    //imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
                    //imagePicker.Canceled += Handle_Canceled;
                    //NavigationController.PresentModalViewController(imagePicker, true);
    
    
    
                    try
                    {
                        var picker = new GMImagePickerController
                        {
                            Title = "Image",
                            CustomDoneButtonTitle = "Done",
                            CustomCancelButtonTitle = "Cancel",
                            CustomNavigationBarPrompt = "Take a new photo or select an existing one!",
                            ColsInPortrait = 3,
                            ColsInLandscape = 5,
                            MinimumInteritemSpacing = 2.0f,
                            DisplaySelectionInfoToolbar = true,
                            AllowsMultipleSelection = true,
                            ShowCameraButton = true,
                            AutoSelectCameraImages = true,
                            AllowsEditingCameraImages = true,
                            ModalPresentationStyle = UIModalPresentationStyle.Popover,
                            MediaTypes = new[] { PHAssetMediaType.Image },
    
    
                        };
    
                        // You can limit which galleries are available to browse through
                        picker.CustomSmartCollections = new[] {
                PHAssetCollectionSubtype.SmartAlbumUserLibrary,
                PHAssetCollectionSubtype.AlbumRegular
            };
    
                        if (_preselectedAssets != null)
                        {
                            foreach (var asset in _preselectedAssets)
                            {
                                picker.SelectedAssets.Add(asset);
                            }
                        }
    
    
                        // Event handling
                        picker.FinishedPickingAssets += Picker_FinishedPickingAssets;
                        picker.Canceled += Picker_Canceled;
    
                        // Other events to implement in order to influence selection behavior:
                        // Set EventArgs::Cancel flag to true in order to prevent the action from happening
                        picker.ShouldDeselectAsset += (s, test1) => { /* allow deselection of (mandatory) assets */ };
                        picker.ShouldEnableAsset += (s, test1) => { /* determine if a specific asset should be enabled */ };
                        picker.ShouldHighlightAsset += (s, test1) => { /* determine if a specific asset should be highlighted */ };
                        picker.ShouldShowAsset += (s, test1) => { /* determine if a specific asset should be displayed */ };
                        picker.ShouldSelectAsset += (s, test1) => { /* determine if a specific asset can be selected */ };
    
                        picker.AssetSelected += (s, test1) => { /* keep track of individual asset selection */ };
                        picker.AssetDeselected += (s, test1) => { /* keep track of individual asset de-selection */ };
    
                        // GMImagePicker can be treated as a PopOver as well:
                        //var popPC = picker.PopoverPresentationController;
                        //popPC.PermittedArrowDirections = UIPopoverArrowDirection.Any;
                        //popPC.SourceView = actionSheet;
                        //popPC.SourceRect = actionSheet.Bounds;
                        ////popPC.BackgroundColor = UIColor.Black;
    
                        await PresentViewControllerAsync(picker, true);
                        //  NavigationController.PresentViewControllerAsync(picker, true);
                        //PresentModalViewController(picker, true);
                    }
                    catch (Exception ex)
                    {
                        CommonHelper.LogException(ex, this);
                    }
    
                    break;
    
                case 1:
    
                    //imagePicker = new UIImagePickerController();
                    //imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
                    //// set what media types
                    //imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary);
                    //imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
                    //imagePicker.Canceled += Handle_Canceled;
                    //NavigationController.PresentModalViewController(imagePicker, true);
    
                    try
                    {
                        var picker = new GMImagePickerController
                        {
                            Title = "Image",
                            CustomDoneButtonTitle = "Done",
                            CustomCancelButtonTitle = "Cancel",
                            CustomNavigationBarPrompt = "Take a new photo or select an existing one!",
                            ColsInPortrait = 3,
                            ColsInLandscape = 5,
                            MinimumInteritemSpacing = 2.0f,
                            DisplaySelectionInfoToolbar = true,
                            AllowsMultipleSelection = true,
                            ShowCameraButton = true,
                            AutoSelectCameraImages = true,
                            AllowsEditingCameraImages = true,
                            ModalPresentationStyle = UIModalPresentationStyle.Popover,
                            MediaTypes = new[] { PHAssetMediaType.Image },
    
    
                        };
    
                        // You can limit which galleries are available to browse through
                        picker.CustomSmartCollections = new[] {
                PHAssetCollectionSubtype.SmartAlbumUserLibrary,
                PHAssetCollectionSubtype.AlbumRegular
            };
    
                        if (_preselectedAssets != null)
                        {
                            foreach (var asset in _preselectedAssets)
                            {
                                picker.SelectedAssets.Add(asset);
                            }
                        }
    
    
                        // Event handling
                        picker.FinishedPickingAssets += Picker_FinishedPickingAssets;
                        picker.Canceled += Picker_Canceled;
    
                        // Other events to implement in order to influence selection behavior:
                        // Set EventArgs::Cancel flag to true in order to prevent the action from happening
                        picker.ShouldDeselectAsset += (s, test1) => { /* allow deselection of (mandatory) assets */ };
                        picker.ShouldEnableAsset += (s, test1) => { /* determine if a specific asset should be enabled */ };
                        picker.ShouldHighlightAsset += (s, test1) => { /* determine if a specific asset should be highlighted */ };
                        picker.ShouldShowAsset += (s, test1) => { /* determine if a specific asset should be displayed */ };
                        picker.ShouldSelectAsset += (s, test1) => { /* determine if a specific asset can be selected */ };
    
                        picker.AssetSelected += (s, test1) => { /* keep track of individual asset selection */ };
                        picker.AssetDeselected += (s, test1) => { /* keep track of individual asset de-selection */ };
    
                        // GMImagePicker can be treated as a PopOver as well:
                        //var popPC = picker.PopoverPresentationController;
                        //popPC.PermittedArrowDirections = UIPopoverArrowDirection.Any;
                        //popPC.SourceView = actionSheet;
                        //popPC.SourceRect = actionSheet.Bounds;
                        ////popPC.BackgroundColor = UIColor.Black;
    
                        await PresentViewControllerAsync(picker, true);
                        //  NavigationController.PresentViewControllerAsync(picker, true);
                        //PresentModalViewController(picker, true);
                    }
                    catch (Exception ex)
                    {
                        CommonHelper.LogException(ex, this);
                    }
                    break;
                case 2:
                    break;
                default:
                    break;
            }
        }
    
        private void Picker_Canceled(object sender, EventArgs e)
        {
    
            if (sender is UIImagePickerController)
            {
                ((UIImagePickerController)sender).DismissViewController(true, null);
            }
    
        }
    
        private void Picker_FinishedPickingAssets(object sender, MultiAssetEventArgs args)
        {
            try
            {
                //bool isImage = false;
                //switch (args.Assets
                //    Info[UIImagePickerController.MediaType].ToString())
                //{
                //    case "public.image":
                //        Console.WriteLine("Image selected");
                //    isImage = true;
                //    break;
                //}
    
                PHImageManager imageManager = new PHImageManager();
    
                Console.WriteLine("User finished picking assets. {0} items selected.", args.Assets.Length);
    
                _preselectedAssets = args.Assets;
    
    
    
                // For demo purposes: just show all chosen pictures in order every second
                foreach (var asset in args.Assets)
                {
                    // imageView.Image = null;
    
                    // Get information about the asset, e.g. file patch
                    asset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(),
                        (input, _) =>
                        {
                            Console.WriteLine(input.FullSizeImageUrl);
                        });
    
                    imageManager.RequestImageForAsset(asset,
                        new CGSize(asset.PixelWidth, asset.PixelHeight),
                        PHImageContentMode.Default,
                        null,
                        (image, info) =>
                        {
                            itemCount += 1;
    
                            indicentImageList.Add(new IndicentImage { IncidentPhoto = image });
    
                        });
    
                    // await Task.Delay(1000);
                }
                //DismissViewController(true, null);
                newIncidentTableView.ReloadSections(NSIndexSet.FromIndex(7), UITableViewRowAnimation.None);
            }
            catch (Exception ex)
            {
                CommonHelper.LogException(ex, this);
            }
        }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    What do you mean by

    Using gmImagePicker i m getting duplicate image if i select

    This plugin was able to let the user choose multiple photos from the gallery. If you select more than one image, you will get several assets in the event Picker_FinishedPickingAssets.

  • AmejackAmejack USMember ✭✭✭

    if i select 2 images i wil get same duplicate images

Sign In or Register to comment.