Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create custom library in iOS?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 329
    Comment on it

    Hi Reader’s,

     

    This blog includes the concept of how to create custom library with the help of AssestsLibrary framework. Custom library is useful for multiple images selection at one time so given below is the screenshot of storyboard followed by code to create custom library :-

     

     

     

     

    As for custom library creation AssestsLibrary framework is used so you have to include this framework as given in screenshot :-

     

     

     

     

    Now given below is the code to create custom library in iOS:-


     

    CustomCollectionViewCell.h
    
    #import <UIKit/UIKit.h>
    #import <AssetsLibrary/AssetsLibrary.h>
    
    @interface CustomCollectionViewCell : UICollectionViewCell
    
    @property (weak, nonatomic) IBOutlet UIImageView *imgView;
    @property(nonatomic, strong) ALAsset *asset;
    
    @end
    
    
    CustomCollectionViewCell.m
    
    #import "CustomCollectionViewCell.h"
    
    @implementation CustomCollectionViewCell
    
    @end
    
    
    
    ViewController.h
    
    
    #import <UIKit/UIKit.h>
    #import <AssetsLibrary/AssetsLibrary.h>
    #import "CustomCollectionViewCell.h"
    
    @interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
    
    @property(nonatomic,assign)id delegate;
    @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
    
    @end
    
    
    ViewController.m
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property(nonatomic, strong) NSArray *assets;
    
    @end
    
    @implementation ViewController
    
    @synthesize delegate;
    
    - (NSManagedObjectContext *)managedObjectContext {
        NSManagedObjectContext *context = nil;
        id delegate = [[UIApplication sharedApplication] delegate];
        if ([delegate performSelector:@selector(managedObjectContext)]) {
            context = [delegate managedObjectContext];
        }
        return context;
    }
    
    + (ALAssetsLibrary *)defaultAssetsLibrary
    {
        static dispatch_once_t pred = nil;
        static ALAssetsLibrary *library = nil;
        dispatch_once(&pred, ^{
            library = [[ALAssetsLibrary alloc] init];
        });
        return library;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        _assets = [@[] mutableCopy];
        __block NSMutableArray *tmpAssets = [@[] mutableCopy];
        
        ALAssetsLibrary *assetsLibrary = [ViewController defaultAssetsLibrary];
        [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if(result)
                {
                    [tmpAssets addObject:result];
                }
            }];
            
            self.assets = tmpAssets;
            [self.collectionView reloadData];
        } failureBlock:^(NSError *error) {
            NSLog(@"Error loading images %@", error);
        }];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        
        return self.assets.count;
        
    }
    
    - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        
        CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        
        ALAsset *asset = self.assets[indexPath.row];
        UIImage *image1=[UIImage imageWithCGImage:[asset thumbnail]];
        cell.imgView.image =image1;
        return cell;
        
    }
    
    - (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
    {
        return 4;
    }
    
    - (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
    {
        return 1;
    }
    
    @end

     

     

    Output:-

     

     

    I hope this blog will help you to understand the concept of how to create custom library in iOS. You can also download the sample project from the link given below:-

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: