0

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

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Amir khan
  • 13
  • 1
  • 8

1 Answers1

0

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

gurmandeep
  • 1,227
  • 1
  • 14
  • 30
  • thank you so much but I don't want to customise it using another view. I'm having this error when try to delete that particular image- Failed to save context : Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" (null) (null) – Amir khan Jun 23 '16 at 06:06
  • You are deleting image from Library with Obj-C code? – gurmandeep Jun 23 '16 at 06:07
  • yeah I found that bug last night. I open UIImagePickerController for selecting image from library and if I select that image and suddenly swap that image at the same time, the image opens in edit view (that feature is inbuilt in iOS) but I don't want that feature in my scenario because if I delete that image from that screen, my app crash. I have gone through this link- http://stackoverflow.com/questions/20270714/why-would-app-try-to-save-to-plsharedmanagedobjectcontext But nothing gonna happens in my case. – Amir khan Jun 23 '16 at 06:24
  • Can you detail me what do you mean by swap? How are you swapping the image – gurmandeep Jun 23 '16 at 06:32
  • swapping means select a image by one finger and then try to swap it right or left with another finger then it goes that edit view. – Amir khan Jun 23 '16 at 06:40
  • Can you share a screenshot of the same? – gurmandeep Jun 23 '16 at 06:49