iOS 多线程实例(自定义NSOperation并传值(block,notification))
来源:互联网 发布:莆田系 知乎 编辑:程序博客网 时间:2024/06/14 20:39
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol DownLoadOperationDelegate <NSObject>
- (void)sendImage:(UIImage *)image;
@end
@interface DownLoadOperation : NSOperation
{
NSString *urlString;
}
//@property (nonatomic,copy)void(^finish)(UIImage *image);
@property (nonatomic)id<DownLoadOperationDelegate>delegate;
//下载图片的operation需要url
- (instancetype)initWithDownLoadMessageURL:(NSString *)url;
@end
#import "DownLoadOperation.h"
@implementation DownLoadOperation
//Operationde具体操作写在main 里面 startde 时候会执行里面的操作
- (instancetype)initWithDownLoadMessageURL:(NSString *)url
{
self = [super init];
if (self) {
urlString = url;
}
return self;
}
- (void)main
{
NSLog(@".......");
// 具体下载的操作
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"%@",image);
[self.delegate sendImage:image];
// [[NSNotificationCenter defaultCenter]postNotificationName:@"下载完成" object:image];
// self.imageBlock(image);
}
@end
#import "ViewController.h"
#import "DownLoadOperation.h"
@interface ViewController ()<DownLoadOperationDelegate>
{
UIImageView *imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listenNot:) name:@"下载完成" object:nil];
imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
[self.view addSubview:imageView];
DownLoadOperation *downLoad = [[DownLoadOperation alloc]initWithDownLoadMessageURL:@"http://www.huabian.com/uploadfile/2014/1118/20141118042246536.jpg"];
downLoad.delegate = self;
// downLoad.imageBlock = ^(UIImage *image){imageView.image = image;};
// [downLoad start];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
queue.maxConcurrentOperationCount = 1;
[queue addOperation:downLoad];
}
- (void)sendImage:(UIImage *)image
{
imageView.image = image;
}
//- (void)listenNot:(NSNotification *)sender
//{
// NSLog(@"%@",sender);
//// imageView.image = sender.object;
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- iOS 多线程实例(自定义NSOperation并传值(block,notification))
- IOS多线程之NSOperation(自定义)
- iOS 多线程(三)NSOperation
- ios 多线程(NSOperation)(swift)
- iOS 多线程(4)NSOperation
- iOS多线程小结( NSOperation)
- iOS 多线程(三)NSOperation
- 自定义NSOperation进行多线程之间的通信(通知、代理、block)
- iOS开发多线程-自定义NSOperation
- iOS多线程系列(2)NSOperation
- (五十六)iOS多线程之NSOperation
- IOS多线程之NSOperation(2)
- iOS多线程学习之NSOperation(一)
- iOS多线程学习之NSOperation(二)
- iOS多线程学习之NSOperation(三)
- iOS开发--多线程编程(三)NSOperation
- iOS多线程编程(三)------ NSOperation
- iOS开发多线程NSOperation(四)
- [软考]之进程调度
- Linux查看和杀死进程
- 使用Java 的Mongo API操作doc
- 连单词成欧拉路 欧拉回路+字典树+并查集 POJ 2513 Colored Sticks
- PAT1008 数组元素循环右移问题
- iOS 多线程实例(自定义NSOperation并传值(block,notification))
- 使用GPU在caffe上进行CNN训练
- Product of Array Except Self
- JavaScript权威指南学习之第5章 语句
- swift的类与对象讲解
- DM6437平台开发-----程序烧写2
- hdu 5428
- RGB颜色与灰度等变换关系
- JAVA实现调整数组顺序使奇数位于偶数前面问题(《剑指 offer》)