Thursday, 15 August 2013

IOS: change UICollectionView Cell size according to the image size

IOS: change UICollectionView Cell size according to the image size

I want to generate an image gallery, without changing the aspect ratio of
the images. Images are in different sizes. I'm using UICollectionView and
positioning an UIImageView inside the cell. I use below code to get the
image size. but it always return size 0.
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString* url = [selectedImages objectAtIndex:indexPath.row];
UIImageView *thumb;
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *myError) {
NSLog(@"failed loading asset");
};
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset) {
[thumb setImage:[UIImage imageWithCGImage:[myAsset
aspectRatioThumbnail]]];
};
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib assetForURL:[NSURL URLWithString:url] resultBlock:resultBlock
failureBlock:failureBlock];
NSLog(@"%f,%f",thumb.image.size.height,thumb.image.size.width);
return thumb.image.size;
}

No comments:

Post a Comment