Rectangle 27 1

ios UICollectionview set cell height based on image height?


Write in ViewDidLoad Method

    for (int i=0; i<[imageURLArray count]; i++)
        {
             //Declare Globally
            IMGArray1=[[NSMutableArray alloc] init];

            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[imageURLArray objectAtIndex:i]]];
            NSOperationQueue *urlQue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:urlRequest queue:urlQue completionHandler:^(NSURLResponse *response,
                                                                                                 NSData *MoblieDatadata, NSError *error)
             {
                 if ([MoblieDatadata length] >0 && error == nil)
                 {
                     UIImage *image = [[UIImage alloc]initWithData:MoblieDatadata];
                     [IMGArray1 addObject:image];

                 }
                 else if ([MoblieDatadata length] == 0 && error == nil)
                 {

                     NSLog(@"Nothing was downloaded.");



                 }
                 else if (error != nil)
                 {
                     NSLog(@"Error happened = %@", error);
                     dispatch_async(dispatch_get_main_queue(), ^{


                     });
                 }
             }];


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

        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        myCell.imageView.image = image;

        return myCell;
    }
    #pragma mark -
    #pragma mark UICollectionViewFlowLayoutDelegate

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        return image.size;
    }

//For setting spacing between collectionview cells
//use the delegate method like

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
  {
    return 10; // This is the minimum inter item spacing, can be more
  }

And Use this for verticle line spacing between cells

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

Well i've not used this, please give me time.Or you can take idea from this :)

i have said in question that i have tried this and it is not working

you have tried this because it is not working giving me error NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0xa781820

Note
Rectangle 27 1

ios UICollectionview set cell height based on image height?


Write in ViewDidLoad Method

    for (int i=0; i<[imageURLArray count]; i++)
        {
             //Declare Globally
            IMGArray1=[[NSMutableArray alloc] init];

            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[imageURLArray objectAtIndex:i]]];
            NSOperationQueue *urlQue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:urlRequest queue:urlQue completionHandler:^(NSURLResponse *response,
                                                                                                 NSData *MoblieDatadata, NSError *error)
             {
                 if ([MoblieDatadata length] >0 && error == nil)
                 {
                     UIImage *image = [[UIImage alloc]initWithData:MoblieDatadata];
                     [IMGArray1 addObject:image];

                 }
                 else if ([MoblieDatadata length] == 0 && error == nil)
                 {

                     NSLog(@"Nothing was downloaded.");



                 }
                 else if (error != nil)
                 {
                     NSLog(@"Error happened = %@", error);
                     dispatch_async(dispatch_get_main_queue(), ^{


                     });
                 }
             }];


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

        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        myCell.imageView.image = image;

        return myCell;
    }
    #pragma mark -
    #pragma mark UICollectionViewFlowLayoutDelegate

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        return image.size;
    }

//For setting spacing between collectionview cells
//use the delegate method like

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
  {
    return 10; // This is the minimum inter item spacing, can be more
  }

And Use this for verticle line spacing between cells

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

Well i've not used this, please give me time.Or you can take idea from this :)

i have said in question that i have tried this and it is not working

you have tried this because it is not working giving me error NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0xa781820

Note
Rectangle 27 1

ios UICollectionview set cell height based on image height?


Write in ViewDidLoad Method

    for (int i=0; i<[imageURLArray count]; i++)
        {
             //Declare Globally
            IMGArray1=[[NSMutableArray alloc] init];

            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[imageURLArray objectAtIndex:i]]];
            NSOperationQueue *urlQue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:urlRequest queue:urlQue completionHandler:^(NSURLResponse *response,
                                                                                                 NSData *MoblieDatadata, NSError *error)
             {
                 if ([MoblieDatadata length] >0 && error == nil)
                 {
                     UIImage *image = [[UIImage alloc]initWithData:MoblieDatadata];
                     [IMGArray1 addObject:image];

                 }
                 else if ([MoblieDatadata length] == 0 && error == nil)
                 {

                     NSLog(@"Nothing was downloaded.");



                 }
                 else if (error != nil)
                 {
                     NSLog(@"Error happened = %@", error);
                     dispatch_async(dispatch_get_main_queue(), ^{


                     });
                 }
             }];


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

        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        myCell.imageView.image = image;

        return myCell;
    }
    #pragma mark -
    #pragma mark UICollectionViewFlowLayoutDelegate

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UIImage *image;
        int row = [indexPath row];

        image = [UIImage imageNamed:IMGArray1[row]];

        return image.size;
    }

//For setting spacing between collectionview cells
//use the delegate method like

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
  {
    return 10; // This is the minimum inter item spacing, can be more
  }

And Use this for verticle line spacing between cells

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}

Well i've not used this, please give me time.Or you can take idea from this :)

i have said in question that i have tried this and it is not working

you have tried this because it is not working giving me error NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0xa781820

Note