how to fetch all media from device using asset library


Hey use this methods to fetch video and photos from photo gallary in iOS 


-(void)getAllVideo

{

    [queue cancelAllOperations];

    queue = [[NSOperationQueue alloc] init];

    [queue addOperationWithBlock:^{

        //imageArray=[[NSArray alloc] init];

        mutableArray =[[NSMutableArray alloc]init];

        NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

        library =[[ALAssetsLibrary alloc] init];

        void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

            if(result != nil) {

                iff([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {

                    

                     [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                      NSURL *url= (NSURL*) [[result defaultRepresentation]url];

                    //NSLog(@”url is %@”,url);

                    NSDate *createdDate=[result valueForProperty:ALAssetPropertyDate];

                    NSMutableDictionary *dicValue=[[NSMutableDictionary alloc] init];

                    [dicValue setObject:[result valueForProperty:ALAssetPropertyDate]  forKey:@”media_created_time”];

                    [dicValue setObject:[NSString stringWithFormat:@”%@”,url]   forKey:@”media_url”];

                    [mutableArray addObject:dicValue];

                }

            }

        }else{

           

        }

   

     

     NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

     void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {

         if(group != nil) {

             [group enumerateAssetsUsingBlock:assetEnumerator];

             [group setAssetsFilter:[ALAssetsFilter allVideos]];

             [assetGroups addObject:group];

             count=(int)[group numberOfAssets];

         }

     };

     assetGroups = [[NSMutableArray alloc] init];

     [library enumerateGroupsWithTypes:ALAssetsGroupAll

                            usingBlock:assetGroupEnumerator

                          failureBlock:^(NSError *error) {NSLog(@”There is an error”);}];

     }];

}


-(void)getAllPictures:(NSDictionary *)bubbleDic

{

    [queue cancelAllOperations];

    queue = [[NSOperationQueue alloc] init];

    

    [queue addOperationWithBlock:^{

        NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

        library =[FetchFromDevice defaultAssetsLibrary];// [[ALAssetsLibrary alloc] init];

        void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

            if(result != nil) {

                if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

                    

                    [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                      NSURL *url= (NSURL*) [[result defaultRepresentation]url];

                    //NSLog(@”url is %@”,url);

                    NSDate *createdDate=[result valueForProperty:ALAssetPropertyDate];

                    NSMutableDictionary *dicValue=[[NSMutableDictionary alloc] init];

                    [dicValue setObject:[result valueForProperty:ALAssetPropertyDate]  forKey:@”media_created_time”];

                    [dicValue setObject:[NSString stringWithFormat:@”%@”,url]   forKey:@”media_url”];

                    [mutableArray addObject:dicValue];

                    

                }

            }else{

                

                

            }

        };

        

        

        NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

        

        void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {

            if(group != nil) {

                

                

                if ([[group valueForProperty:@”ALAssetsGroupPropertyType”] intValue] == ALAssetsGroupSavedPhotos)

                {

                    

                    [group enumerateAssetsUsingBlock:assetEnumerator];

                    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

                    [assetGroups addObject:group];

                    count=(int)[group numberOfAssets];

                }

            }

        };

        

        assetGroups = [[NSMutableArray alloc] init];

        

        [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos

                               usingBlock:assetGroupEnumerator

                             failureBlock:^(NSError *error) {NSLog(@”There is an error %@”,error);}];

        }];

}

2. how to fetch image from AlassetUrl

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

    NSString *imageStr=Your Url;

    [library assetForURL:[NSURL URLWithString:imageStr]

             resultBlock:^(ALAsset *asset) {

                 NSLog(@”fetchinging PHAssets…wwwwwwwwwwwwwww…………”);

                 

                 ALAssetRepresentation *rep = [asset defaultRepresentation];

                 @autoreleasepool {

                     CGImageRef iref = [rep fullScreenImage];

                     if (iref) {

                         UIImage *image = [UIImage imageWithCGImage:iref];

                     }

                 }

             }

            failureBlock:^(NSError *error){ NSLog(@”operation was not successfull!”);

            }];


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: