[TwistedFate]图片异步加载,KVO

本文详细介绍了如何使用自定义类ImageDownLoader实现异步下载图片的功能,并结合KVO键值观察技术实时更新UI状态。通过初始化、加载数据、异常处理等步骤,展示了异步操作在图片加载中的高效应用。同时,文章还通过实例演示了如何在控制器中观察并响应被观察者的属性变化,以实现动态UI更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异步下载图片

自定义类imageDownLoader类

思路:
- 异步下载图片首先需要一个网址字符串 (需要传入一个字符串)
- 加载完图片数据需要将一个data传给controller显示(代理传值)
- 考虑到异步加载可能成功,也可能失败
代码实现:
- 定义协议

//  创建一个协议
@protocol ImageDownLoadDelegate <NSObject>

//  请求成功
- (void)imageDownLoadSucceedWithData:(NSData *)data;

//  请求失败
- (void)imageDownLoadFailedWithError:(NSError *)error;

@end
  • 初始化需要的属性及方法
@interface ImageDownLoader : NSObject
//  连接
@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, retain) NSMutableData *data;

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

//  自定义初始化
- (instancetype)initWithUrl:(NSString *)url delegate:(id<ImageDownLoadDelegate>) delegate;

//  申明开始的方法
- (void)imageDownLoadStart;

//  终止方法
- (void)imageDownLoadCancel;
@end
  • 方法实现
//  初始化方法(需要写请求中 可能用到的参数网址)
- (instancetype)initWithUrl:(NSString *)url delegate:(id<ImageDownLoadDelegate>) delegate{

    self = [super init];

    if (self) {

        //  设置代理
        self.delegate = delegate;

        //  创建一个网址对象
        NSURL *newUrl = [NSURL URLWithString:url];

        //  根据网址创建一个请求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];

        //  创建链接(异步代理)
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

        //[self.connection start];

    }

    return self;

}
  • 代理方法实现
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    self.data = [NSMutableData data];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{


    [self.data appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    //  图片请求完成+
    if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoadSucceedWithData:)]) {

        //  调用 协议中的方法
        [_delegate imageDownLoadSucceedWithData:self.data];

    }

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    if (_delegate != nil && [_delegate respondsToSelector:@selector(imageDownLoadFailedWithError:)]) {

        [_delegate imageDownLoadFailedWithError:error];

    }

}

- (void)imageDownLoadStart{

    [self.connection start];

}

- (void)imageDownLoadCancel{

    [self.connection cancel];

}

KVO键值观察者

1.创建一个被观察者类ManModel

//  创建一个对象
    self.man = [[ManModel alloc] init];
    self.man.money = @"121312";

 /**
     *  KVO键值观察者
     *  观察model中的某一个属性 值的变化
     *  如果这个属性的值 发生变化 会触发一个方法
     *  明确:
     *  1.观察者   (控制器中触发一个方法 -- 比如改变视图的颜色)
     *  2.被观察者 (man)
     *  3.观察的属性 (对象中的 money这个属性)

     *  控制器作为观察者 去观察 model(被观察者)中的一个属性的值的变化 会触发一个方法(往往是改变视图的)

     *  观察者(Controller)
     *  被观察者(Model)
     *  Model 发生变化 去改变  视图(V)
     */

 //  添加一个观察者
    //  被观察者 man
    //  Observer 填观察者
    //  keyPath 填属性名
    //  NSKeyValueObservingOptionOld原有值
    //  NSKeyValueObservingOptionNew新值
    //  context携带参数
    [self.man addObserver:self forKeyPath:@"money" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:@"haha"];

添加一个button点击改变man的money值

- (void)click:(UIButton *)button{

    self.man.money = @"das";

}

被观察者改变触发的方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    //  哪个属性发生了变化
    NSLog(@"%@",keyPath);
    //  object被观察的对象
    NSLog(@"%@",object);
    //  change值的变化
    NSLog(@"%@",change);
    //  context携带的参数
    NSLog(@"%@",context);

    //  移除观察者
    [object removeObserver:self forKeyPath:keyPath];

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值