0

Hi
I am getting a problem in load images from the gallery to UIImageView in Xcode 5.

When I am trying to get images from the Gallery in UIImage it will generate exceptional error in my screen.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Ankit
  • 286
  • 4
  • 16

2 Answers2

4

You can use following method to load image from gallery. You have to call methodToCallPhotos when you click on gallery button.

-(void)methodToCallPhotos
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self presentModalViewController:imagePicker animated:YES];

}

After this when you select any image from the gallery. It calls delegate method like below

-(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    imageView.image = image;
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Leri
  • 12,367
  • 7
  • 43
  • 60
Code Hunter
  • 10,075
  • 23
  • 72
  • 102
0

First of all check if your app is granted permission to access Photo gallery.

Have a look at the documentation for ALAssetsLibrary here. To access all photos and videos you need to enumerate all groups (albums) in the photo library and then enumerate all photos and images in each group.

ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];

[al enumerateGroupsWithTypes:ALAssetsGroupAll

    usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
        {
            if (asset)
            {                
                 .. do something with the asset
            }
        }
    }

    failureBlock:^(NSError *error)
    {
        .. handle error 
    }
 ];
tek3
  • 2,095
  • 2
  • 20
  • 50