使用串行线程实现图片瀑布流加载

.h文件

typedef NS_ENUM(NSInteger, LowerList) {
    Left = 1,
    Right = 2
};

@interface VC_AddStoryModel : UIViewController
{
    UIScrollView *scroll;
    LowerList lower;
    CGFloat viewsWidth;
    UIView *leftView;
    CGFloat leftHeight;
    
    UIView *rightView;
    CGFloat rightHeight;
    BOOL firstPicture;
}

.h文件

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];

    {
        viewsWidth = ScreenWidth / 2;

        firstPicture = YES;

        scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
        scroll.contentSize = CGSizeMake(ScreenWidth, ScreenHeight);
        scroll.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:scroll];
        
        leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewsWidth, 0)];
        rightView = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH / 2, 0, viewsWidth, 0)];
        
        [scroll addSubview:leftView];
        [scroll addSubview:rightView];
        scroll.backgroundColor = [UIColor yellowColor];
    }
    [self layoutImage];
    
    
}
- (void)layoutImage {
    
    NSArray* urls = @[
                      @"http://img0.bdstatic.com/img/image/shouye/leimu/mingxing.jpg",
                      @"http://img1.bdstatic.com/img/image/8662934349b033b5bb5c55e5d9834d3d539b700bcce.jpg",
                      @"http://img.baidu.com/img/image/3bf33a87e950352a5947ae485143fbf2b2.jpg",
                      @"http://imgstatic.baidu.com/img/image/7af40ad162d9f2d3cdc19be8abec8a136227cce1.jpg",
                      @"http://imgstatic.baidu.com/img/image/weimeiyijing0207.jpg",
                      @"http://e.hiphotos.baidu.com/image/w%3D400/sign=2e56c8010ed79123e0e095749d355917/ae51f3deb48f8c5470385d2638292df5e1fe7fd4.jpg",
                      @"http://c.hiphotos.baidu.com/image/w%3D400/sign=e37cc47c6509c93d07f20ff7af3cf8bb/7a899e510fb30f2468cc6271ca95d143ad4b0369.jpg",
                      @"http://b.hiphotos.baidu.com/image/w%3D400/sign=ac0b8e2b92ef76c6d0d2fa2bad17fdf6/a71ea8d3fd1f4134dedc5974271f95cad0c85ed4.jpg",
                      @"http://imgstatic.baidu.com/img/image/huacaozhiwu0207.jpg",
                      @"http://d.hiphotos.baidu.com/image/w%3D400/sign=7d27c75af4246b607b0eb374dbf81a35/5882b2b7d0a20cf4f28367d674094b36acaf99ac.jpg",
                      ];
    dispatch_queue_t queue = dispatch_queue_create("get image", DISPATCH_QUEUE_SERIAL);
    for (int i = 0; i < 100; i++) {
        __block  UIImage *image;
        @synchronized(scroll) {
            dispatch_async(queue, ^{
                NSString *imageUrl = urls[i % 10];
                NSURL *url = [NSURL URLWithString:imageUrl];
                NSData *data = [NSData dataWithContentsOfURL:url];
                image = [UIImage imageWithData:data];
            });
            dispatch_async(queue, ^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self makePictuerVisible:image];
                });
            });
       }
    }
}

- (void)makePictuerVisible:(UIImage *)image {
    lower = [self checkList];
    CGFloat width = viewsWidth;
    CGFloat height = width * image.size.height / image.size.width;
    UIImageView *picture = nil;
    switch (lower) {
        case Left:
        {
            picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, leftView.frame.size.height, width, height)];
            leftView.frame = CGRectMake(leftView.frame.origin.x, leftView.frame.origin.y, viewsWidth, leftView.frame.size.height + height);
            [leftView addSubview:picture];
        }
            break;
        case Right:
            picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, rightView.frame.size.height, width, height)];
            rightView.frame = CGRectMake(rightView.frame.origin.x, rightView.frame.origin.y, viewsWidth, rightView.frame.size.height + height);
            [rightView addSubview:picture];
            
            break;
        default:
            break;
    }
    picture.image = image;
    if (Left == [self checkList]) {
        [scroll setContentSize:CGSizeMake(ScreenWidth, rightView.frame.size.height + 1)];
    } else {
        [scroll setContentSize:CGSizeMake(ScreenWidth, leftView.frame.size.height + 1)];
    }
    
    NSLog(@"%f   %f",scroll.contentSize.height, scroll.contentSize.width);
}
- (LowerList)checkList {
    LowerList temp;
    if (firstPicture) {
        temp = Left;
        firstPicture = NO;
    } else {
        leftHeight = leftView.frame.size.height;
        rightHeight = rightView.frame.size.height;
        if (leftHeight > rightHeight) {
            temp = Right;
        } else {
            temp = Left;
        }
    }
    return temp;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

实现的效果是顺序把图片添加进界面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ai迷惑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值