爱悠闲 > IOS 图片加载封装

IOS 图片加载封装

分类: 图片加载  |  标签: 图片加载  |  作者: lllqqqqqqqqq 相关  |  发布日期 : 2015-03-11  |  热度 : 117°

图片加载,根据图片网络地址,加载出图片, 我们把这个方法封装在一个类里面:ImageDownLoader

1. 创建ImageDownLoader类


//  ImageDownLoader.h

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@class ImageDownLoader;

@protocol ImageDownLoadDelegate <NSObject>


#warning 1.执行协议, 第一个参数是imageDownLoad对象 第二个是另一个页面需要用得UIImage对象(出口值)

- (void)imageDownload:(ImageDownLoader *)imageDownLoad didFinishLoading:(UIImage *)image;


@end

@interface ImageDownLoader : NSObject

@property (nonatomic, assign)id<ImageDownLoadDelegate>imageDownLoadDelegate;


#warning 2.自定义初始化方法 作用 在创建这个类的对象的时候将图片的URL传过来(入口值)

// 将代理作为参数传进来 功能等同于 imageDownLoader.imageDelegate =self;

- (instancetype)initWithImageUrlStr:(NSString *)urlStr imageDownLoadDelegate:(id<ImageDownLoadDelegate>)imageDelegate;

@end



//  ImageDownLoader.m

#import "ImageDownLoader.h"


@implementation ImageDownLoader

#warning 3.实现自定义初始化方法 作用 在创建这个类的对象的时候将图片的URL传过来

- (instancetype)initWithImageUrlStr:(NSString *)urlStr imageDownLoadDelegate:(id<ImageDownLoadDelegate>)imageDelegate

{

    self = [super init];

    if (self) {

        NSURL *url = [NSURL URLWithString:urlStr];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

            UIImage *image = [UIImage imageWithData:data];

#warning 4.判断代理人是否为空 且代理方法是否存在

            if (imageDelegate != nil && [imageDelegate respondsToSelector:@selector(imageDownload:didFinishLoading:)]) {

                [imageDelegate imageDownload:self didFinishLoading:image];

            }

        }];

    }

    return self;

}

@end


2. 实现imageDownLoader后, 我们来测试一下
(1)在APPDelegate里指定一个根视图控制器

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    RootViewController *rootVC = [[RootViewController alloc] init];

    self.window.rootViewController = rootVC;

    [rootVC release];


    return YES;

}


(2)在根视图控制器里,创建一个button

//

//  RootViewController.h


#import <UIKit/UIKit.h>

#import "ImageDownLoader.h"

@interface RootViewController : UIViewController<ImageDownLoadDelegate>

@property (nonatomic, retain)UIImageView *imageView;

@end



//  RootViewController.m

#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

  

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 180, 200, 150)];

    self.imageView.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:self.imageView];

    

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.view addSubview:button1];

    button1.frame = CGRectMake(180, 100, 80, 50);

    button1.backgroundColor = [UIColor greenColor];

    [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside ];

    [button1 setTitle:@"下载图片" forState:UIControlStateNormal];

    

}

#warning 6. 实现协议方法  得到图片

-(void)imageDownload:(ImageDownLoader *)imageDownLoad didFinishLoading:(UIImage *)image

{

    self.imageView.image = image;

}

- (void)buttonAction1:(UIButton *)button

{

#warning 5.传图片URL给工具类

    NSString *urlStr = @"

http://f11.topit.me/o/201008/20/12822784561644.jpg

" ;

    ImageDownLoader *imageDownLoader = [[ImageDownLoader alloc] initWithImageUrlStr:urlStr imageDownLoadDelegate:self];


}


运行看一下结果


同类文章:图片加载