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;
}
You always got image size return size 0 because your UIImageview
() get image in ^{} block
and it is not executed on mainthread
, so, you have to calculate image size before sizeForItemAtIndexPath
this method or
you can try like this, but i guess it is not proper way,
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset) {
[thumb setImage:[UIImage imageWithCGImage:[myAsset aspectRatioThumbnail]]];
return thumb.image.size;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.