IOS 图片加载封装

来源:互联网 发布:淘宝的店铺经营许可 编辑:程序博客网 时间:2024/04/26 09:10

图片加载,根据图片网络地址,加载出图片, 我们把这个方法封装在一个类里面: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 = [superinit];

   if (self) {

       NSURL *url = [NSURLURLWithString:urlStr];

        NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];

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

           UIImage *image = [UIImageimageWithData:data];

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

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

                [imageDelegateimageDownload:selfdidFinishLoading: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 = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    

    RootViewController *rootVC = [[RootViewControlleralloc] init];

    self.window.rootViewController = rootVC;

    [rootVCrelease];


    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 {

    [superviewDidLoad];

  

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

    self.imageView.backgroundColor = [UIColorlightGrayColor];

    [self.viewaddSubview:self.imageView];

    

    UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [self.viewaddSubview:button1];

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

    button1.backgroundColor = [UIColorgreenColor];

    [button1 addTarget:selfaction:@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 = [[ImageDownLoaderalloc] initWithImageUrlStr:urlStrimageDownLoadDelegate:self];


}


运行看一下结果



0 0
原创粉丝点击