zoukankan      html  css  js  c++  java
  • 李洪强iOS开发之

     李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片

     源码: 

    //

    //  ViewController.m

    //  08-九宫格扩展

    //

    //  Created by 李洪强 on 15/6/21.

    //  Copyright (c) 2015 李洪强. All rights reserved.

    //

     

    #define kWIDTH [UIScreen mainScreen].bounds.size.width

    #define kHEIGHT [UIScreen mainScreen].bounds.size.height

    #define kMARGIN 3

    #define kROWS 5

    #define kCOL 3

    #define kNUMBERS 100

     

    #import "ViewController.h"

    #import "UIImageView+WebCache.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;

        NSLog(@"%@",path);

        

        NSLog(@"viewDidLoadB");

        // Block 闭包 - 从函数外部可以访问函数内部的变量.

        

        // 每一个格子的宽/

        

        CGFloat imageW = (kWIDTH - kMARGIN*(kCOL +1))/kCOL;

        

        CGFloat imageH = (kHEIGHT - kMARGIN*(kROWS +1))/kROWS;

        

        for (int i = 0; i < 15; i++) {

            

            // 确定格子所在的行和列

            

            int col = i % kCOL;

            int row = i / kCOL;

            

            // 确定每一个格子的XY

            CGFloat imageX = kMARGIN + (kMARGIN + imageW)*col;

            

            CGFloat imageY = kMARGIN + (kMARGIN + imageH)*row;

            

            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];

            

            imageView.backgroundColor = [UIColor greenColor];

            

            

            NSString *urlString = [NSString stringWithFormat:@"http://images.cnblogs.com/cnblogs_com/kenshincui/613474/o_%d.jpg",i];

            

            NSURL *url = [NSURL URLWithString:urlString];

            

            [self.view addSubview:imageView];

            

            [imageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                // 设置进度条

    //            // 创建一个进度条

    //            UIProgressView *progress = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, imageW, 2)];

    //            // 设置进度条的值

    //            progress.progress = (double)receivedSize/expectedSize;

    //            // 设置进度条的颜色

    //            progress.tintColor = [UIColor redColor];

    //            

    //            [imageView addSubview:progress];

                

                UIView *progressView = [[UIView alloc] init];

                progressView.backgroundColor = [UIColor redColor];

                

                CGFloat width = (double)receivedSize/expectedSize *imageW;

                

                progressView.frame = CGRectMake(0, 0, width, 2);

                

                [imageView addSubview:progressView];

                

            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                //完成回调

                NSLog(@"执行完毕v");

            }];

            

        }

    }

     

     

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

    @end

  • 相关阅读:
    使用某些 DOCTYPE 时会导致 document.body.scrollTop 失效
    VB.NET 笔记1
    知识管理系统Data Solution研发日记之一 场景设计与需求列出
    知识管理系统Data Solution研发日记之五 网页下载,转换,导入
    折腾了这么多年的.NET开发,也只学会了这么几招 软件开发不是生活的全部,但是好的生活全靠它了
    分享制作精良的知识管理系统 博客园博客备份程序 Site Rebuild
    知识管理系统Data Solution研发日记之四 片段式数据解决方案
    知识管理系统Data Solution研发日记之二 应用程序系列
    知识管理系统Data Solution研发日记之七 源代码与解决方案
    知识管理系统Data Solution研发日记之三 文档解决方案
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/5793481.html
Copyright © 2011-2022 走看看