zoukankan      html  css  js  c++  java
  • MBProgressHUD多线程加载图片

    本文原创,转载请注明出处!

    冻死我了,没钱吃饭了,作为猪来说我鸭梨巨大。饥寒交迫的我为了大家能够更好地学习,苦命地写博客。

    下面为大家呈上MBProgressHUD的两种图片加载提示效果,一种是在一个线程里加载,另一种是开一个线程加载,并使用MBProgressHUD作为等待的提示框,下面请看效果图:

    在使用之前请先下载MBProgressHUD类,下载地址是: http://github.com/matej/MBProgressHUD      下载完直接把MBProgressHUD.hMBProgressHUD.m两个文件拖到项目中

    第一种方式,只在在主线程里加载:

    请看代码

    #import <UIKit/UIKit.h>
    #import "MBProgressHUD.h"
    
    @interface ViewController : UIViewController<MBProgressHUDDelegate>
    {
        MBProgressHUD *_progress;
    }
    @property(retain,nonatomic)MBProgressHUD *progress;
    @property(retain,nonatomic)IBOutlet UIImageView *image;
    - (void)requestImage;@end
    #import "ViewController.h"
    
    @implementation ViewController
    @synthesize progress = _progress;
    @synthesize image;
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        self.progress = [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:self.progress];
        self.progress.delegate = self;
        self.progress.labelText = @"加载中......";
        [self.progress showWhileExecuting:@selector(requestImage) onTarget:self withObject:nil animated:YES];//一边加载一边执行里面的requestImage方法
        [super viewDidLoad];//其实这个方法也是新开一个线程,不过这个方法被封装在类里面了,大家可不必了解
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)requestImage
    {
        NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.jiankangzu.com/pian/03/201103291210451233.jpg"]];
        image = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];
        image.frame = CGRectMake(0, 0, 320, 480);
        [self.view addSubview:image];
    }
    
    - (void)dealloc {
        [image release];
        image = nil;
        [super dealloc];
    }
    
    @end

    代码很简单不解释了,下面来看多线程的

    ViewController.h文件一样,这里省略。

    ViewController.m文件如下:

    #import "ViewController.h"
    
    @implementation ViewController
    @synthesize progress = _progress;
    @synthesize image;
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        self.progress = [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:self.progress];
        self.progress.delegate = self;
        self.progress.labelText = @"加载中......";
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //新开一个线程加载
        [self.progress show:YES];
        [NSThread detachNewThreadSelector:@selector(requestImage) toTarget:self withObject:nil];
    }
    
    - (void)requestImage
    {
    
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.jiankangzu.com/pian/03/201103291210451233.jpg"]];
        image = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]];
        //在另一个线程更新主界面,因为在本函数里更新主界面是无效的
        [self performSelectorOnMainThread:@selector(displayImage) withObject:nil waitUntilDone:NO];
        [pool drain];
    }
    
    //显示图片信息
    -(void)displayImage
    {
        //若self.progressHUD为真,则将self.progressHUD移除,设为nil
        if (self.progress){
            [self.progress removeFromSuperview];
            [self.progress release];
            self.progress = nil;
        }
        [self.view addSubview:image];
        [self.image setFrame:CGRectMake(0, 0, 320, 480)];
    }
    
    - (void)dealloc {
        [image release];
        [super dealloc];
    }

    两种方法的加载速度有什么差别呢?大家可以去尝试一下,如果图片多的话那一种会比较快。不过两种其实都是采用多线程加载,而第一种比较简洁,我建议大家用第一种方式。

    冻死了,赶紧睡觉去,期待大家的留言......

     
     
  • 相关阅读:
    其他
    聚类算法:ISODATA算法
    大神博客
    Fiddldr 教程之:HTTP协议详解(转)
    设计模式之装饰模式的复习
    NOIP 2011 聪明的质监员
    CSP-S2020/NOIP2020模板总结(Updating)
    CSP-S2020/NOIP2020复习指南
    洛谷 U137412 高斯的小宇宙
    NOIP2020模板测试题大全
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/MBProgressHUD.html
Copyright © 2011-2022 走看看