lundi 29 décembre 2014

Inserting different sizes Images in UICollectionView In iOS

I am currently working in an iOS application in which i have to show images in collection view. I am working with following procedure :




  1. I am using below code for saving the image in collectionviewcontroller from my current controller.


    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; layout.itemSize=CGSizeMake(320, 420); layout.minimumLineSpacing = -8.0; [layout setSectionInset:UIEdgeInsetsMake(0, 30, 0, 0)]; CollectionViewController *coll=[[CollectionViewController alloc]initWithCollectionViewLayout:layout ]; [self.navigationController pushViewController:coll animated:YES];




2: In collectionviewcontroller, i am inserting images with following method.



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
int i;
Cell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:REUSEABLE_CELL_IDENTITY forIndexPath:indexPath];

cell.layer.borderWidth=8.0f;

@try
{
NSLog(@"index path is %ld",(long)indexPath.row);
items =[ChildDetails getAllData];
cdata =[AlbumDetails getAllData];
revArray = [ChildDetails getAllDataDesc];

cell.bounds=CGRectMake(0,0, 320, 420);

if(rearr.count == 0)
{
NSLog(@"Do nothing");
cell.imageviews.image=NULL;
}
else
{

NSLog(@"count %lu",(unsigned long)rearr.count);
for (i=0;i<rearr.count; i++)
{
NSLog(@"count %lu",(unsigned long)rearr.count);
image = [UIImage imageWithContentsOfFile:rearr[i]];
if(image != nil)
{
[itms addObject:image];
}
else
{
NSData *data = [[NSData alloc]initWithContentsOfFile:rearr[i]];
image=[UIImage imageWithData:data];
[itms addObject:image];
}
}

cell.imageviews.contentMode = UIViewContentModeScaleAspectFit ;
cell.imageviews.clipsToBounds=YES;

cell.imageviews.image=[itms objectAtIndex:indexPath.row];
}


return cell;
}
@catch (NSException *exception)
{
return cell;
}
}


Everything works fine , but the problem is when i insert the images whose width is greater than the height, in that scenario i get the white spaces in the cell. I have attach the image of my collection viewcontroller, so you can easily understand my problem. I had tried with the below code also :



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

return CGSizeMake(image.size.width, image.size.height);
}


I Know this is the only way by which i can get out from the issue but i am not getting the proper way how to use it for every image.


My requirement is when i select and insert different sizes images it will fit there properly.There will not be any white space between two images ,![I want to make the gallery like this in collection view so when i save any image of any size it will fit properly in the gallery.][1]


Please guide me the way.I am highly grateful to you.




Aucun commentaire:

Enregistrer un commentaire