0

I know this question might be asked many time but i am a beginner so kindly being gentle is a humble request.

I am trying to make an image gallery which displays all the images Of a URL.

I have used all asset framework and made a simple gallery which return the images of camera roll, I want to have the image of some URL so kindly help me to Get images from url and display them to Image Gallery

Here Is the view Controller header File

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController : UIViewController <UICollectionViewDelegate,UICollectionViewDataSource>
@property (retain, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic, strong) NSArray *assets;

Here is the Implementation file In which I have registered An .NIB file to add Cell to my CollectionView

#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "MyCell.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];





_assets = [@[] mutableCopy];
__block NSMutableArray *tmpAssets = [@[] mutableCopy];
// 1

here it is calliing defaultAssetsLibrary method which you can see later

 ALAssetsLibrary *assetsLibrary = [ViewController defaultAssetsLibrary];
    // 2
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {


  if(result)
            {

                // 3
                [tmpAssets addObject:result];
            }
        }];

        // 4
        //NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
        //self.assets = [tmpAssets sortedArrayUsingDescriptors:@[sort]];
        self.assets = tmpAssets;

        // 5
        [self.collectionView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"Error loading images %@", error);
    }];



}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - collection view data source


- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.assets.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:



- (MyCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];

    ALAsset *asset = self.assets[indexPath.row];
    cell.asset = asset;
    cell.backgroundColor = [UIColor redColor];
    ////
    //=


    cell.cellLabel.text = [NSString stringWithFormat:@"cell %i",indexPath.row];
    return cell;
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ALAsset *asset = self.assets[indexPath.row];
    ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
    UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

    // Do something with the image

}

And here is that method which I think returning the photo library

+ (ALAssetsLibrary *)defaultAssetsLibrary
{


    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred, ^{
        library = [[ALAssetsLibrary alloc] init];
    });
    return library;
}

Please Help me to get Images From URL and display them in Image Gallery.

As Far as MyCell file is concern.

Here is MYCell.m file

@interface MyCell ()
// 1
@property(nonatomic, strong) IBOutlet UIImageView *photoImageView;
@end

@implementation MyCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) setAsset:(ALAsset *)asset
{


    // 2
    _asset = asset;
    self.photoImageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
    }
Exception
  • 249
  • 1
  • 4
  • 17
  • 1
    It's really bad form to delete a question and then ask it again. All of the helpful comments on your last question are now lost. Don't waste people's time. – rmaddy Dec 05 '13 at 22:41
  • I tried to post a better question I know It should have NSURl but i want to know what should be edited to the existing code as am not very expert like you fellow @rmaddy and I hope solution to this question will be answer for dozen of people queries. – Exception Dec 05 '13 at 22:48
  • Is your code successfully displaying images saved to your camera roll and you only want to save all images from a url to your camera roll and display accordingly from there? – Ayan Sengupta Dec 05 '13 at 23:02
  • no i just want to display images from url bhai @AyanSengupta – Exception Dec 05 '13 at 23:06
  • and yes it is displaying images stored at camera roll @AyanSengupta – Exception Dec 05 '13 at 23:24
  • First I dont find any reason you should use ALAssetLibrary for that. If you only want to display images from a url to your table view, I think this [link](http://www.raywenderlich.com/4295) might help you. Its a bit exhaustive but very much efficient. Happy coding. – Ayan Sengupta Dec 05 '13 at 23:30
  • I dont know the reason but i want something like gallery U know like [this](https://dl.dropboxusercontent.com/u/2857188/RecipePhoto_UICollectionView.zip) so that the images displayed are in format of gallery – Exception Dec 05 '13 at 23:53
  • Dont understand what you are up to? You only need to populate your images in UICollectionView instead of UITableView and all the remaining processes should be the same from the link I've given above. – Ayan Sengupta Dec 06 '13 at 00:00
  • i didnt get the point " I want to have the image of some URL"... – Shamsudheen TK Dec 09 '13 at 03:08
  • This tutorial describes well,[**creating-an-image-gallery**](http://brandontreb.com/iphone-programming-tutorial-creating-an-image-gallery-like-over-part-1). – Shamsudheen TK Dec 09 '13 at 03:15

1 Answers1

0

If we are getting images from URL we don't need AlAsset for image gallery really.

I have followed this tutorial to make the image gallery Guide and Simple passed the array of url and it worked for me.

Exception
  • 249
  • 1
  • 4
  • 17