ios get all pictures in mobile phone

Here I use AssetsLibrary to get all the pictures in the phone, no more nonsense.
First we have to import the relevant library

#import <AssetsLibrary/AssetsLibrary.h>

code:

photo = [[NSMutableArray alloc]init];

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

    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(dispatchQueue, ^(void){    

            // Loop through all albums

            [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){    

                [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){

                    NSString *assetType = [result valueForProperty:ALAssetPropertyType];            

                    NSDictionary *url = [result valueForProperty:ALAssetPropertyURLs];

                    if ([assetType isEqualToString:ALAssetTypePhoto])

                    {

                        [photo addObject:url];

                        NSLog(@"url == %@", url);

                    }

                }];

                dispatch_async(dispatch_get_main_queue(), ^{

                    if (photo != nil)

                    {

                        //I don't know if there is any harm in calling here

                    }

                    

                });

                

            }

                                      failureBlock:^(NSError *error)

             {

                 NSLog(@"failed");

             }];

    });

In this way, the urls of all our pictures are placed in the photo array. If we call:

 UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];

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

        [lib assetForURL:[[photo objectAtIndex:0] valueForKey:@"public.jpeg"] resultBlock:^(ALAsset *asset)

         {

//Here you can get the relevant information of the photo

             ALAssetRepresentation *assetRep = [asset defaultRepresentation];

//Get the thumbnail

             CGImageRef imgRef = asset.thumbnail;

             imgView.image = [UIImage imageWithCGImage:imgRef

                                                 scale:assetRep.scale

                                           orientation:(UIImageOrientation)assetRep.orientation];

         }

            failureBlock:^(NSError *error)

         {

             NSLog(@"failed!!");

         }];


Guess you like

Origin blog.csdn.net/Twan1234/article/details/48490099