iOS NSOperation 异步加载图片 封装NSOperation 代理更新
#import <Foundation/Foundation.h>
@class MYOperation;
@protocol MYOperationDelecate <NSObject>
-(void)operationWithStr:(UIImage*)str;
@end
@interface MYOperation : NSOperation
@property(nonatomic,copy)NSString *imageURL;
@property(nonatomic,weak)id<MYOperationDelecate>delegate;
@end
#import "MYOperation.h"
@implementation MYOperation
//必须实现main方法
-(void)main
{
@autoreleasepool {
//模拟下载图片返回的字符串在主进程中返回到控制器进行跟新操作
UIImage*str=[self downloadImage];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
if([self.delegate respondsToSelector:@selector(operationWithStr:)])
{
[self.delegate operationWithStr:str];
}
}];
};
}
//模拟下载图片
-(UIImage*)downloadImage
{
NSURL *url=[NSURL URLWithString:self.imageURL];
NSData *data=[NSData dataWithContentsOfURL:url];
UIImage*image=[UIImage imageWithData:data];
return image;
}
@end
//控制器加载代码
#import "ViewController.h"
#import "MYOperation.h"
@interface ViewController ()<MYOperationDelecate>
@property(nonatomic,strong)NSOperationQueue*operation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
MYOperation *operation=[[MYOperation alloc]init];
operation.delegate=self;
operation. imageURL=@"www.baidu.com/image";
[self.operation addOperation:operation];
}
-(void)operationWithStr:(UIImage*)str
{
#warning 在这里实现UI界面的更新
NSLog(@"%@,%@",str,[NSThread currentThread]);
}
-(NSOperationQueue *)operation
{
if(!_operation)
{
_operation=[[NSOperationQueue alloc]init];
[_operation setMaxConcurrentOperationCount:6];
}
return _operation;
}
@end
iOS NSOperation 异步加载图片 封装NSOperation 代理更新的更多相关文章
- ios UITableView 异步加载图片并防止错位
UITableView 重用 UITableViewCell 并异步加载图片时会出现图片错乱的情况 对错位原因不明白的同学请参考我的另外一篇随笔:http://www.cnblogs.com/lesl ...
- IOS学习之路二十三(EGOImageLoading异步加载图片开源框架使用)
EGOImageLoading 是一个用的比较多的异步加载图片的第三方类库,简化开发过程,我们直接传入图片的url,这个类库就会自动帮我们异步加载和缓存工作:当从网上获取图片时,如果网速慢图片短时间内 ...
- IOS中UITableView异步加载图片的实现
本文转载至 http://blog.csdn.net/enuola/article/details/8639404 最近做一个项目,需要用到UITableView异步加载图片的例子,看到网上有一个E ...
- 多线程异步加载图片async_pictures
异步加载图片 目标:在表格中异步加载网络图片 目的: 模拟 SDWebImage 基本功能实现 理解 SDWebImage 的底层实现机制 SDWebImage 是非常著名的网络图片处理框架,目前国内 ...
- 实例演示Android异步加载图片
本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...
- 实例演示Android异步加载图片(转)
本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...
- android listview 异步加载图片并防止错位
网上找了一张图, listview 异步加载图片之所以错位的根本原因是重用了 convertView 且有异步操作. 如果不重用 convertView 不会出现错位现象, 重用 convertVie ...
- android异步加载图片并缓存到本地实现方法
图片过多造成内存溢出,这个是最不容易解决的,要想一些好的缓存策略,比如大图片使用LRU缓存策略或懒加载缓存策略.今天首先介绍一下本地缓存图片 在android项目中访问网络图片是非常普遍性的事 ...
- [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html 这个可以实现ImageView异步加载 ...
随机推荐
- bzoj 3110 K大数查询
第一道整体二分,因为只需要知道每个询问区间中比mid大的数有多少个,就可以直接用线段树区间加,区间求和了. #include<iostream> #include<cstdio> ...
- windows 下 新建 点开头的文件和文件夹
新建 .aaa文件夹 cmd:$ mkdir .aaa 新建 .aaa文件夹 echo " >> .aaa
- windows下pip安装python模块时报错总结
http://www.cnblogs.com/liaojiafa/p/5100550.html 前言: 这几天把python版本升级后,发现pip安装模块好多都报错(暂不确定是不是因为升级导致的),我 ...
- EF唯一索引
this.Property(p => p.Name) .IsRequired() .HasMaxLength()) .HasColumnAnnotation("Index", ...
- Android接入支付宝和银联
支付宝接入参考链接:https://software.intel.com/zh-cn/node/542608 银联接入参考链接:http://blog.csdn.net/gf771115/articl ...
- IIS ARR 负载均衡
阅读:http://www.cnblogs.com/jesse2013/p/dlws-loadbalancer2.html 自定义端口:http://www.th7.cn/Program/net/20 ...
- hadoop集群安装_实战
spark1.6.2+ hadoop2.6.2 词频统计完整案例:http://blog.csdn.net/zythy/article/details/17852579 hadoop学习:http:/ ...
- storyboard连线容易出现的问题
- 连接的方法代码被删掉,但是连线没有去掉 - 可能会出现方法找不到错误 - unrecognized selector sent to instance- 连接的属性代码被删掉,但是连线没有去掉 - ...
- Tools
Database: Online Schema Tool: http://dbdsgnr.appspot.com/ Orcale --> SQL developer MySQL--> To ...
- 微信小程序实质是什么? Hybrid App
微信小程序是一种不需要下载安装即可使用的应用,用户扫一扫或者搜一下即可打开应用.微信小程序实质是Hybrid技术的应用.Hybrid App(混合模式移动应用). 小程序能够更多的可以更多的调用手机本 ...