IOS network image loading framework (4 methods in total)
Framework name: UIImage + WebCache. h inherited from UIimageView
In the framework, there are four methods for loading network images: 1. normal loading 2. Thread NSThread 3.
# Import "ViewController. h"
# Import "UIImage + WebCache. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
ImageArray = [NSMutableArray array];
For (int I = 0; I <5; I ++ ){
For (int j = 0; j <6; j ++ ){
UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (j * 65 + 5, I * 64 + 10, 60, 60)];
ImageView. backgroundColor = [UIColor cyanColor];
[Self. view addSubview: imageView];
[ImageArray addObject: imageView];
}
}
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = CGRectMake (160,400, 80, 30 );
[Button setTitle: @ "loading" forState: UIControlStateNormal];
[Button addTarget: self action: @ selector (buttonAction) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
}
-(Void) buttonAction {
For (UIImageView * imageView in imageArray ){
NSString * string = @ "http://img3.douban.com/view/photo/photo/public/p2221339563.jpg ";
NSURL * url = [NSURL URLWithString: string];
[ImageView setImageWithURL: url];
}
}
UIImage + WebCache. h custom Method
# Import
@ Interface UIImageView (WebCache)
-(Void) setImageWithURL :( NSURL *) url;
@ End
Implementation of the UIImage + WebCache. m -------- Method
# Import "UIImage + WebCache. h"
@ Implementation UIImageView (WebCache)
-(Void) setImageWithURL :( NSURL *) url {
// Method 1: normal loading
/*
NSData * data = [NSData dataWithContentsOfURL: url];
UIImage * image = [UIImage imageWithData: data];
[Self setImage: image];
*/
// Method 2: Thread
// NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (loadImage :) object: url];
// [Thread start];
// Method 3: queue
/* NSOperationQueue * queue = [[NSOperationQueue alloc] init];
// Operands
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock: ^ {
NSData * data = [NSData dataWithContentsOfURL: url];
UIImage * image = [UIImage imageWithData: data];
// Use the main queue column to return to the main thread
// Modify the UI on the main thread
[[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
// Modify the UI
[Self setImage: image];
}];
}];
[Queue addOperation: op];
*/
// ---------------- GCD ---------------
/// 1. Create a Queue
// Dispatch_queue_t queue = dispatch_queue_create ("com. xw. gcd. queue", DISPATCH_QUEUE_CONCURRENT); // parallel queue
//
/// 2. Create the task to be executed and add it to the queue
// Dispatch_async (queue, ^ {
//
// NSData * data = [NSData dataWithContentsOfURL: url];
// UIImage * image = [UIImage imageWithData: data];
// [Self setImage: image];
//});
// Use the queue provided by the system ---
// (Two parameters)
// Parameter 1: Priority
// Parameter 2: identifier
Dispatch_queue_t globalQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );
Dispatch_async (globalQueue, ^ {
// Request data
NSData * data = [NSData dataWithContentsOfURL: url];
UIImage * image = [UIImage imageWithData: data];
// Display on the UI
Dispatch_async (dispatch_get_main_queue (), ^ {
// UI-related code
// It is best to refresh the UI to the main thread.
[Self setImage: image];
});
});
}
-(Void) loadImage :( NSURL *) url {
NSData * data = [NSData dataWithContentsOfURL: url];
UIImage * image = [UIImage imageWithData: data];
[Self setImage: image];
}
@ End