How to prevent user to select and swap images at a same time from image library so that it doesn't to go edit view?

79 Views Asked by At

I'm having a problem regarding selecting image from image library in iPhone(iOS 8.4).

Here is my code:

   UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
   imagePicker.delegate = self;
   imagePicker.allowsEditing = NO;
   imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
   [self presentModalViewController:imagePicker animated:YES];

But if I select an image and swap that image at the same time it opens an edit view and then if I try to delete that image, my app crash.

Is it a default feature of image library? Or can it be handled by code?

Please let me know. Thanks in advance

1

There are 1 best solutions below

6
gurmandeep On

If you could try

NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
    void (^assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
    {
        if(group != nil)
        {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
             {
                 //ALAssetRepresentation holds all the information about the asset being accessed.
                 if(result)
                 {
                     ALAssetRepresentation *representation = [result defaultRepresentation];

                     if (representation !=nil)
                     {
                         UIImage *PhotoThumbnail = [UIImage imageWithCGImage:[result thumbnail]];
                         [fetchedThumbnails addObject:PhotoThumbnail];
                         UIImage * latestPhoto = [UIImage imageWithCGImage:[result thumbnail]];
                         [fetchedImages addObject:latestPhoto];
                     }
                 }
             }];
            [assetGroups addObject:group];
        }

        galleryImagesCV.hidden=NO;
        [galleryImagesCV reloadData];
    };

    assetGroups = [[NSMutableArray alloc] init];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    NSUInteger groupTypes = ALAssetsGroupSavedPhotos;

    [library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:^(NSError *error)
     {
        NSLog(@"A problem occurred");
     }];

import AssetsLibrary/AssetsLibrary.h

That will give you all images from gallery in arrays. You can show these in a custom view and select/swap etc.

Hope it helps