動態(tài)獲取網(wǎng)絡(luò)圖片高度

1、先獲取圖片,獲取高度

2、給model賦值,刷新tableView,重新加載圖片;

#import "ViewController.h"

#import "ItemCell.h"

#import "ItemModel.h"

#import"SDWebImage/UIImageView+WebCache.h"

#import"CHTCollectionViewWaterfallLayout.h"

@interface ViewController ()@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;


@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.title = @"瀑布流";

[self initData];

CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];

layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

//? ? layout.headerHeight = 15;

//? ? layout.footerHeight = 10;

//? ? layout.minimumColumnSpacing = 20;

//? ? layout.minimumInteritemSpacing = 30;

self.collectionView.collectionViewLayout = layout;

self.collectionView.delegate = self;

self.collectionView.dataSource = self;

self.collectionView.backgroundColor = [UIColor whiteColor];

[self.collectionView registerClass:[ItemCell class] forCellWithReuseIdentifier:@"item"];

}

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

[self updateLayoutForOrientation:[UIApplication sharedApplication].statusBarOrientation];

}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

[self updateLayoutForOrientation:toInterfaceOrientation];

}

- (void)updateLayoutForOrientation:(UIInterfaceOrientation)orientation {

CHTCollectionViewWaterfallLayout *layout =

(CHTCollectionViewWaterfallLayout *)self.collectionView.collectionViewLayout;

layout.columnCount = UIInterfaceOrientationIsPortrait(orientation) ? 2 : 3;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)initData {

NSArray *imageArray = @[

@"http://g.hiphotos.baidu.com/image/w%3D310/sign=6f9ce22079ec54e741ec1c1f89399bfd/9d82d158ccbf6c81cea943f6be3eb13533fa4015.jpg",

@"http://b.hiphotos.baidu.com/image/pic/item/4bed2e738bd4b31cc6476eb985d6277f9e2ff8bd.jpg",

@"http://c.hiphotos.baidu.com/image/pic/item/94cad1c8a786c9174d18e030cb3d70cf3bc7579b.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=79bc1b1a950a304e5222a6fbe1c9a7c3/d1160924ab18972b50a46fd4e4cd7b899e510a15.jpg",

@"http://c.hiphotos.baidu.com/image/w%3D310/sign=05e2c867272dd42a5f0907aa333a5b2f/7dd98d1001e93901f3f7103079ec54e737d196c3.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=3914596cf1deb48ffb69a7dfc01e3aef/d0c8a786c9177f3ea3e73f0072cf3bc79e3d56e8.jpg",

@"http://c.hiphotos.baidu.com/image/w%3D310/sign=8cc67b8cc91349547e1eee65664e92dd/4610b912c8fcc3ce11e40a3e9045d688d43f2093.jpg",

@"http://c.hiphotos.baidu.com/image/w%3D310/sign=93e1c429952bd40742c7d5fc4b889e9c/3812b31bb051f8191cdd594bd8b44aed2e73e733.jpg",

@"http://b.hiphotos.baidu.com/image/pic/item/4bed2e738bd4b31cc6476eb985d6277f9e2ff8bd.jpg",

@"http://c.hiphotos.baidu.com/image/pic/item/94cad1c8a786c9174d18e030cb3d70cf3bc7579b.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=3914596cf1deb48ffb69a7dfc01e3aef/d0c8a786c9177f3ea3e73f0072cf3bc79e3d56e8.jpg",

@"http://c.hiphotos.baidu.com/image/w%3D310/sign=93e1c429952bd40742c7d5fc4b889e9c/3812b31bb051f8191cdd594bd8b44aed2e73e733.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=d4507def9d3df8dca63d8990fd1072bf/d833c895d143ad4b758c35d880025aafa40f0603.jpg",

@"http://c.hiphotos.baidu.com/image/w%3D310/sign=702acce2552c11dfded1b92253266255/d62a6059252dd42a3ac70aaa013b5bb5c8eab8e0.jpg",

@"http://h.hiphotos.baidu.com/image/w%3D310/sign=75ff59cd19d5ad6eaaf962ebb1ca39a3/b64543a98226cffcb9f3ddbbbb014a90f703eada.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=11386163f1deb48ffb69a7dfc01e3aef/d0c8a786c9177f3e8bcb070f72cf3bc79f3d5631.jpg",

@"http://f.hiphotos.baidu.com/image/w%3D310/sign=8ed508bbd358ccbf1bbcb33b29d8bcd4/8694a4c27d1ed21b33ff8fecaf6eddc451da3f80.jpg",

@"http://b.hiphotos.baidu.com/image/w%3D310/sign=ad40ca82c9ef76093c0b9f9e1edca301/5d6034a85edf8db16aa7b27b0b23dd54564e7420.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=79bc1b1a950a304e5222a6fbe1c9a7c3/d1160924ab18972b50a46fd4e4cd7b899e510a15.jpg",

@"http://c.hiphotos.baidu.com/image/w%3D310/sign=05e2c867272dd42a5f0907aa333a5b2f/7dd98d1001e93901f3f7103079ec54e737d196c3.jpg",

@"http://g.hiphotos.baidu.com/image/w%3D310/sign=6f9ce22079ec54e741ec1c1f89399bfd/9d82d158ccbf6c81cea943f6be3eb13533fa4015.jpg",

@"http://e.hiphotos.baidu.com/image/w%3D310/sign=79bc1b1a950a304e5222a6fbe1c9a7c3/d1160924ab18972b50a46fd4e4cd7b899e510a15.jpg"

];

self.dataArray = [NSMutableArray array];

for (NSString *item in imageArray) {

ItemModel *model = [ItemModel new];

model.imageUrl = item;

[self.dataArray addObject:model];

}

}

#pragma mark - UICollectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return self.dataArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

ItemCell *cell = (ItemCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"item"

forIndexPath:indexPath];

ItemModel *model = [self.dataArray objectAtIndex:indexPath.row];

NSString *imgUrlString = model.imageUrl;

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imgUrlString] placeholderImage:[UIImage imageNamed:@"loading"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

if (image) {

if (!CGSizeEqualToSize(model.imageSize, image.size)) {

model.imageSize = image.size;

[collectionView reloadItemsAtIndexPaths:@[indexPath]];

}

}

}];

return cell;

}

#pragma mark - CHTCollectionViewDelegateWaterfallLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

ItemModel *model = [self.dataArray objectAtIndex:indexPath.row];

if (!CGSizeEqualToSize(model.imageSize, CGSizeZero)) {

return model.imageSize;

}

return CGSizeMake(150, 150);

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 傳動 傳動的部分嚴(yán)格的來說只包括 :牙盤 鏈條 中軸 和飛輪其實腳踏是不包含在傳動當(dāng)中的.但是我也放在傳動里講. ...
    去屎吧閱讀 8,989評論 0 15
  • 吳老頭在村西臨近國道的十字路口搭了一個簡易的石棉瓦棚房賣餛飩,在篷房的柱子上,貼了一張大紙,說自己的兒子在村西口的...
    金大錚閱讀 234評論 0 0
  • 其實一直以來不敢承認(rèn)自己很多事都不行,現(xiàn)在要學(xué)會習(xí)慣很多不行,我確實不夠努力不夠堅持,心思很亂,記憶力也不行,但是...
    6186834d3810閱讀 226評論 0 0
  • 文. 朱湘怡 媽媽就像一本厚厚的“百科全書”讀懂她這本書,可真不容易。 媽媽這本書充滿了溫暖。我喜歡吃的飯...
    六片楓葉閱讀 653評論 3 2
  • 這些照片都是出自于澳洲女?dāng)z影師Jane Long根據(jù)老照片翻新過來的,Jane Long以大約一個半世紀(jì)前的老照片...
    米拍閱讀 308評論 2 3

友情鏈接更多精彩內(nèi)容