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
    • 355
    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:-


     

    1. CustomCollectionViewCell.h
    2.  
    3. #import <UIKit/UIKit.h>
    4. #import <AssetsLibrary/AssetsLibrary.h>
    5.  
    6. @interface CustomCollectionViewCell : UICollectionViewCell
    7.  
    8. @property (weak, nonatomic) IBOutlet UIImageView *imgView;
    9. @property(nonatomic, strong) ALAsset *asset;
    10.  
    11. @end
    12.  
    13.  
    14. CustomCollectionViewCell.m
    15.  
    16. #import "CustomCollectionViewCell.h"
    17.  
    18. @implementation CustomCollectionViewCell
    19.  
    20. @end
    21.  
    22.  
    23.  
    24. ViewController.h
    25.  
    26.  
    27. #import <UIKit/UIKit.h>
    28. #import <AssetsLibrary/AssetsLibrary.h>
    29. #import "CustomCollectionViewCell.h"
    30.  
    31. @interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
    32.  
    33. @property(nonatomic,assign)id delegate;
    34. @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
    35.  
    36. @end
    37.  
    38.  
    39. ViewController.m
    40.  
    41. #import "ViewController.h"
    42.  
    43. @interface ViewController ()
    44.  
    45. @property(nonatomic, strong) NSArray *assets;
    46.  
    47. @end
    48.  
    49. @implementation ViewController
    50.  
    51. @synthesize delegate;
    52.  
    53. - (NSManagedObjectContext *)managedObjectContext {
    54. NSManagedObjectContext *context = nil;
    55. id delegate = [[UIApplication sharedApplication] delegate];
    56. if ([delegate performSelector:@selector(managedObjectContext)]) {
    57. context = [delegate managedObjectContext];
    58. }
    59. return context;
    60. }
    61.  
    62. + (ALAssetsLibrary *)defaultAssetsLibrary
    63. {
    64. static dispatch_once_t pred = nil;
    65. static ALAssetsLibrary *library = nil;
    66. dispatch_once(&pred, ^{
    67. library = [[ALAssetsLibrary alloc] init];
    68. });
    69. return library;
    70. }
    71.  
    72. - (void)viewDidLoad {
    73. [super viewDidLoad];
    74. _assets = [@[] mutableCopy];
    75. __block NSMutableArray *tmpAssets = [@[] mutableCopy];
    76. ALAssetsLibrary *assetsLibrary = [ViewController defaultAssetsLibrary];
    77. [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    78. [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
    79. if(result)
    80. {
    81. [tmpAssets addObject:result];
    82. }
    83. }];
    84. self.assets = tmpAssets;
    85. [self.collectionView reloadData];
    86. } failureBlock:^(NSError *error) {
    87. NSLog(@"Error loading images %@", error);
    88. }];
    89. // Do any additional setup after loading the view, typically from a nib.
    90. }
    91.  
    92. - (void)didReceiveMemoryWarning {
    93. [super didReceiveMemoryWarning];
    94. // Dispose of any resources that can be recreated.
    95. }
    96.  
    97.  
    98. - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    99. {
    100. return self.assets.count;
    101. }
    102.  
    103. - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    104. {
    105. CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    106. ALAsset *asset = self.assets[indexPath.row];
    107. UIImage *image1=[UIImage imageWithCGImage:[asset thumbnail]];
    108. cell.imgView.image =image1;
    109. return cell;
    110. }
    111.  
    112. - (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
    113. {
    114. return 4;
    115. }
    116.  
    117. - (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
    118. {
    119. return 1;
    120. }
    121.  
    122. @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
Reset Password
Fill out the form below and reset your password: