IOS nsoperation Asynchronous Load Picture Encapsulation Nsoperation Agent Update

Source: Internet
Author: User

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.