#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
The main method must be implemented
-(void) Main
{
@autoreleasepool {
Simulates downloading a picture the returned string is returned to the controller in the main process to be followed by the new operation
Uiimage*str=[self Downloadimage];
[[Nsoperationqueue mainqueue]addoperationwithblock:^{
if ([Self.delegate respondstoselector: @selector (operationwithstr:)])
{
[Self.delegate OPERATIONWITHSTR:STR];
}
}];
};
}
Simulate download pictures
-(uiimage*) downloadimage
{
Nsurl *url=[nsurl urlwithstring: Self.imageurl];
NSData *data=[nsdata Datawithcontentsofurl:url];
Uiimage*image=[uiimage Imagewithdata:data];
return image;
}
@end
Controller Load Code
#import "ViewController.h"
#import "MYOperation.h"
@interface Viewcontroller () <MYOperationDelecate>
@property (Nonatomic,strong) nsoperationqueue*operation;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
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. [Email protected] "www.baidu.com/image";
[Self.operation addoperation:operation];
}
-(void) Operationwithstr: (uiimage*) str
{
#warning here to implement updates to the UI interface
NSLog (@ "%@,%@", Str,[nsthread CurrentThread]);
}
-(Nsoperationqueue *) operation
{
if (!_operation)
{
_operation=[[nsoperationqueue Alloc]init];
[_operation Setmaxconcurrentoperationcount:6];
}
return _operation;
}
@end
IOS nsoperation loading picture nsoperation agent updates asynchronously