//NSThread_多線程開發
#import "ViewController.h"
#define ROW 5
#define COLUMN 3
#define IMAGE_COUNT ROW*COLUMN
#define WIDTH 100//圖片寬
#define HEIGHT WIDTH//圖片高
@interface ViewController ()
{
NSMutableArray *_imageViews;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
}
- (void)layout
{
_imageViews = [NSMutableArray array];
for (int r = 0; r < COLUMN; r++) {
for (int c = 0; c < ROW; c++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(18.75 + r * 118.75, 20 + c * 118.75, WIDTH, HEIGHT)];
imageView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:imageView];
[_imageViews addObject:imageView];
}
}
//添加按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 500,275,30);
[button setTitle:@"加載圖片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(useMultiThread) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)updateImage : (NSArray *)arr
{
UIImage *image = [UIImage imageWithData:arr[0]];
UIImageView *imageView = _imageViews[[arr[1] intValue]];
imageView.image = image;
}
- (void)loadImage : (NSNumber *)index{
NSLog(@"%@", [NSThread currentThread].name);
if (![index isEqual:@14]) {
//若是當前進程不是最後一個,就休眠2秒
[NSThread sleepForTimeInterval:2];
}
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://i1.s2.dpfile.com/2009-12-16/3351675_b.jpg(700x700)/thumb.jpg"]];
NSArray *arrData = @[data,index];
[self performSelectorOnMainThread:@selector(updateImage:) withObject:arrData waitUntilDone:YES];
}
//- (void)useMultiThread
//{
// for (int i = 0; i < IMAGE_COUNT; i++) {
// NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
// thread.name = [NSString stringWithFormat:@"個人線程:%d",i];
// [thread start];//啓動
// }
//}
#pragma mark 改變線程優先級
//提升他被優先加載的概率,可是他也未必就第一個加載。1,其餘進程是先啓動的 2.網絡狀態咱們沒有辦法修改
- (void)useMultiThread{
NSMutableArray *threads = [NSMutableArray array];
for(int i = 0; i < IMAGE_COUNT ; i++){
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
//設置優先級
if (i ==IMAGE_COUNT - 1){
thread.threadPriority = 1.0;
}
else{
thread.threadPriority = 0;
}
[threads addObject:thread];
}
for (int i = 0; i < IMAGE_COUNT; i++) {
NSThread *thread = threads[i];
[thread start];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
c++
//
// ViewController.m
// NSOperation
//
// Created by DC020 on 15/12/24.
// Copyright (c) 2015年 Bill. All rights reserved.
//
#import "ViewController.h"
#define ROW 3
#define COLUMN 5
#define imageCount ROW*COLUMN
#define WIDTH 100
#define HEIGHT WIDTH
@interface ViewController ()
{
NSMutableArray *_imageViews;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
}
-(void)layout{
_imageViews = [NSMutableArray array];
for (int c = 0; c < COLUMN; c++) {
for (int r = 0; r<ROW; r++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(18.75+r*118.75, 20+c*118.75, WIDTH, HEIGHT)];
image.backgroundColor = [UIColor redColor];
[self.view addSubview:image];
[_imageViews addObject:image];
}
}
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 500, 275, 30)];
[button setTitle:@"加載圖片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(userMultiThread) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)updateImage:(NSArray *)arr{
UIImage *image = [UIImage imageWithData:arr[0]];
UIImageView *imageView = _imageViews[[arr[1] intValue]];
imageView.image = image;
}
-(void)loadImage:(NSNumber *)index{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://b.zol-img.com.cn/desk/bizhi/image/7/2560x1600/1447404575613.jpg"]];
//更新UI界面,調用主線程隊列
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateImage:@[data,index]];
}];
}
-(void)userMultiThread{
//1.建立一個操做隊列
NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
operationQueue.maxConcurrentOperationCount = 5;//設置最大併發線程數;
//2.向隊列添加操做
// for(int i = 0;i < imageCount; i++){
//方法1.建立操做塊,添加到隊列
// NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
// [self loadImage:[NSNumber numberWithInt:i]];
// }];
// //向操做隊列 添加 操做
// [operationQueue addOperation:blockOperation];
//方法2:禱文invocation
// NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImage:) object:[NSNumber numberWithInt:i]];
// [operationQueue addOperation:invocationOperation];
//方法3:控制線程的執行順序
NSBlockOperation *lastOp = [NSBlockOperation blockOperationWithBlock:^{
[self loadImage:@(imageCount - 1)];
}];
for (int j = 0; j < imageCount-1; j++) {
NSBlockOperation *normalOp = [NSBlockOperation blockOperationWithBlock:^{
[self loadImage:[NSNumber numberWithInt:j]];
}];
//設置依賴操做(普通依賴最後)
[normalOp addDependency:lastOp];
[operationQueue addOperation:normalOp];
}
[operationQueue addOperation:lastOp];
}
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
網絡
//
// ViewController.m
// 自帶地圖應用_路徑導航
//
// Created by DC020 on 15/12/24.
// Copyright (c) 2015年 Bill. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController (){
CLGeocoder *_geocoder;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_geocoder = [[CLGeocoder alloc]init];
[self drawRoute];
}
-(void)drawRoute{
//根據「西安」進行地理編碼
[_geocoder geocodeAddressString:@"西安市" completionHandler:^(NSArray *placemarks, NSError *error) {
//定位地標
CLPlacemark *clPlacemark1 = [placemarks firstObject];//通常來講第一個是關聯度最高的
//定位地標轉化爲地圖地標
MKPlacemark *mkPlacemark1 = [[MKPlacemark alloc]initWithPlacemark:clPlacemark1];
//注意:地理編碼一次只能定位到一個位置
[_geocoder geocodeAddressString:@"哈爾濱市" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *clPlacemark2 = [placemarks firstObject];
MKPlacemark *mkplacemark2 = [[MKPlacemark alloc]initWithPlacemark:clPlacemark2];
//配置地圖設置信息
NSDictionary *options =@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};//將其轉化成對象類型(加括號的意思)
//將地標轉化成地圖上的「點」
MKMapItem *mapItem1 = [[MKMapItem alloc]initWithPlacemark:mkPlacemark1];
MKMapItem *mapItem2 = [[MKMapItem alloc]initWithPlacemark:mkplacemark2];
//加載地圖(點,設置的選項)
[MKMapItem openMapsWithItems:@[mapItem1,mapItem2] launchOptions:options];
}];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
多線程
//
// ViewController.m
// NSThread
//
// Created by DC020 on 15/12/24.
// Copyright (c) 2015年 Bill. All rights reserved.
//
#import "ViewController.h"
@interface ViewController (){
//用於顯示圖片
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self layout];
}
-(void)layout{
_imageView = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds] ];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:_imageView];
//下載按鈕
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 500, 275, 30)];
[button setTitle:@"加載圖片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(loadImageWithMultiThread) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
-(void)noNSThread{
//在資源下載加載的過程當中,因爲網絡緣由,有時候咱們是很難保證下載時間的,若是不使用多線程可能用戶完成一個下載操做須要很長時間的等待,並且在這個過程當中沒法進行其餘操做。->阻塞
UIImage *image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://i-7.vcimg.com/trim/867a1fe997bf3baeebbd223ae9aecdc8142598/trim.jpg"]]];
_imageView.image = image;
}
-(void)updateImageView:(NSData *) imageData{
UIImage *image = [UIImage imageWithData:imageData];
_imageView.image = image;
}
-(void)loadImage{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://i-7.vcimg.com/trim/867a1fe997bf3baeebbd223ae9aecdc8142598/trim.jpg"]];
//注意只能在主線程上才能更新UI
//performSelectorOnMainThread是NSObject一個方法,每個NSObject都有這個方法
[self performSelectorOnMainThread:@selector(updateImageView:) withObject:data waitUntilDone:YES];
}
#pragma mark 多線程下載圖片
-(void)loadImageWithMultiThread{
//方法1:使用對象方法
//建立一個線程
// NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(loadImage) object:nil];
// [thread1 start];//啓動一個線程,啓動一個線程並不表明當即執行,而是出於就緒狀態,當系統調度時才真正執行
//方法2:使用類方法
[NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
併發