// 下载 - > 合并 - > 显示
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 组 // 一个并行队列队列中 有两个任务 然后放入一个组中
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block UIImage *image1 = nil;
dispatch_group_async(group,queue, ^{
NSString *urlString1 = [NSString stringWithFormat:@"http://img04.tooopen.com/images/20130712/tooopen_17270713.jpg"];
NSURL *url1 = [NSURL URLWithString:urlString1];
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url1]];
});
__block UIImage *image2 = nil;
dispatch_group_async(group,queue, ^{
NSString *urlString2 = [NSString stringWithFormat:@"https://www.baidu.com/img/bd_logo1.png"];
NSURL *url2 = [NSURL URLWithString:urlString2];
image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url2]];
});
// 保证执行完组里面的所有任务之后,再执行notify函数里面的block
dispatch_group_notify(group, queue, ^{
UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0f);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = fullImage;
});
UIGraphicsEndImageContext();
});
#if 0
// @"http://img04.tooopen.com/images/20130712/tooopen_17270713.jpg"
// @"https://www.baidu.com/img/bd_logo1.png"
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 下载第一张
NSString *urlString1 = [NSString stringWithFormat:@"http://img04.tooopen.com/images/20130712/tooopen_17270713.jpg"];
NSURL *url1 = [NSURL URLWithString:urlString1];
self.image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url1]];
NSLog(@"---%@---",[NSThread currentThread]);
[self bindImages];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 下载第二张
NSString *urlString2 = [NSString stringWithFormat:@"https://www.baidu.com/img/bd_logo1.png"];
NSURL *url2 = [NSURL URLWithString:urlString2];
self.image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:url2]];
NSLog(@"---%@---",[NSThread currentThread]);
[self bindImages];
});
#endif
}
- (void)bindImages {
if (self.image1 == nil || self.image2 == nil) {
UIGraphicsBeginImageContextWithOptions(self.image1.size, NO, 0.0f);
[self.image1 drawInRect:CGRectMake(0, 0, self.image1.size.width, self.image1.size.height)];
[self.image2 drawInRect:CGRectMake(0, 0, self.image2.size.width, self.image2.size.height)];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = fullImage;
});
UIGraphicsEndImageContext();
} else return;
}
相关推荐
CS231n+assignment2(一)
cs231n作业二详解+代码
iOS:GCD信号量、同步、异步的使用方法
信号量的集体用法
【EPICS学习记录】根据官方教程创建一个IOC应用
参照官网给出的IOC创建教程逐步操作完成了一个IOC应用的创建,官网教程中有部分过程表述的不清晰,自己摸索之后完成了IOC的创建,并把流程记录下来。其中有一些操作不是很理解,有问题或者建议欢迎指正。
并发编程 - GCD队列组
通过本文的介绍,我们深入探讨了GCD队列组这一强大工具在并发编程中的应用。队列组不仅能够帮助我们同步多任务,还能在所有任务完成后执行特定的收尾操作,这在实际开发中非常场景且使用。无论是上传多个媒体资源并在完成后统一发布,还是在处理并发任务时确保所有步骤顺序执行,队列组都能提供简洁而有效的解决方案。但在使用队列组时,务必要注意线程安全,任务进出组的对称性,以及合理选择notify()或wait()方法,以避免潜在的问题。通过正确地掌握和应用队列组,我们可以更好地管理多任务处理,提升代码的可靠性和可维护性。
两轮差速小车循线控制原理分析
硬件资料设定:小车驱动来自于两个相同的电机,转向依靠两轮差速实现,小车前后左右安装超声波传感器,前后各一个,左右各两个; 功能目标:假设小车左侧有墙壁,通过超声波测距实现按指定距离沿墙行驶 参数设定:车轮半径r=3cm,车轮间距wd=10cm,车身长length=16cm,侧边超声波传感器前后各一个,假设一个在车头一个在车尾,间距=车身=length=16cm,小车线速度恒定v = 0.2m/s,距离墙d=10cm,假设超声波传感器以10Hz的频率采集距离。 在以上条件下,设定小车出发点,方向与墙平行
EtherCAT APWR报文实战:从站地址配置(0x0010)的初始化流程与报文解析
本文详细解析了EtherCAT APWR报文在从站地址配置(0x0010寄存器)中的初始化流程与实战应用。通过类比物业分配门牌号,生动解释了APWR报文如何高效批量配置从站地址,并深入探讨了0x0010寄存器的特殊作用、初始化步骤及常见问题排查方法。文章结合IgH主站的三从站配置案例,提供了报文解析技巧和调试建议,帮助工程师快速掌握EtherCAT网络配置的核心技术。
iOS - 多线程-GCD-队列组
开发过程中,有时候想实现这样的效果。并发执行完成后,再并发执行任务。可以使用GCD的队列组来实现。并发执行,完成后执行任务。,达到这样的效果:任务。
Challenge 6 - OSCP C
开放了161端口,直接snmpbulkwalk扫描得到账号密码试了那几个web端口,发现可以登录8083的vesta,但是需要用户名大写Jack接着登录之后里面可以创建计划任务,直接弄个反弹shell,一分钟回弹。得到shell后,运行linpeas.sh提供了几个可能的漏洞,挨个试一下。
iOS开发—使用GCD实现多线程开发—调度队列组
假设有一个音乐应用,如果要执行多个下载歌曲的任务,这些耗时的任务会被放到多个线程上异步执行,指导全部的歌曲下载完成,弹出一个提示框来通知用户歌曲已下载完成。 针对这个应用场景,可以考虑使用队列组。一个队列组可以将多个block组成一组,用于监听这一组任务是否全部完成,指导关联的任务全部完成后再发出通知以执行其他的操作。iOS提供了如下的函数开始用队列组。 (1)创建队列组 要想使用队列组,首
【回眸】Tessy集成测试软件使用指南(一)新手使用篇
这个专栏的文章前4篇都在记录如何使用Tessy进行单元测试,集成测试需要有一定单元测试基础,且做集成测试之前,需要做好单元测试,否则将会大幅增加软件单元代码纠错的代价。集成测试所花费的时间通常远远超过单元测试。如果直接从单元测试到系统测试,结果往往是不理想的。集成测试可以检测由于单元相互交互而导致的错误,通常由一组专业的测试人员执行。
duilib 实现截屏选取功能分析
本文关注点不再截屏而是截屏选取工具 1首先肯定是个透明窗口,且置顶并且是工具窗口(任务栏不显示窗口图标) 窗口样式 <?xml version="1.0" encoding="utf-8"?> <Window shadowattached="false"> <Box > <VBox name="track_box" margin="200,200,200,200" bkimage="file='screen_bord...
GCD基本使用总结
1.GCD的基本使用 1.1 同步执行 + 串行队列 总结:所有任务都是在主线程完成的,证明了同步执行不具备开启新的线程。所有任务都是按序执行。 1.2 同步执行+并发队列 总结:所有任务都是在主线程中执行的,没有开启新的线程,也就是说同步不具备开启新线程的能力。虽然并发队列可以同时执行多个任务,但是当前只有一个线程,一个线程只能执行一个任务,所以这里也就不存在并发队列。 ...
PHP实现SM2算法(附完整源码)
PHP实现SM2算法(附完整源码)
网络多线程-GCD队列组
#import "ViewController.h" @interface ViewController () /** 图片1*/ @property(nonatomic ,strong) UIImage *image1; /** 图片2*/ @property(nonatomic ,strong) UIImage *image2; @pro
OV9734 DATASHEET,完整规格书
OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等
ET2012新版免狗免安装绿色软件
ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,
多线程(二):GCD的基本使用,同步函数和异步函数,串行队列和并发队列,GCD队列组,栅栏函数,GCD单例
什么是GCD 1)全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 2)纯C语言,提供了非常多强大的函数 GCD的优势 1)GCD是苹果为多核的并行运算提出的解决方案。 2)GCD会自动利用更多的CPU内核(比如双核,四核)。 3)GCD会自动管理线程的生命周期(创建线程,调度任务)。 任务和队列 1)任务:执...
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
GCD队列组
GCD队列组 有时候我们会有这样的需求:分别异步执行2个耗时操作,然后当2个耗时操作都执行完毕后再回到主线程执行操作。这时候我们可以用到GCD的队列组,我们可以先把任务放到队列中,然后将队列放入队列组中,调用队列组的dispatch_group_notify回到主线程执行操作 - (void)queueGroup { //创建队列组 dispatch_group_t que
Data-Structures-and-Algorithm-Analysis-in-Java.pdf
作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...
iOS swift-GCD-队列组
情境:在异步中完成所有操作后再在主线程里完成其它操作 比如这个例子:先根据图片链接下载完所有的图片后(异步操作),再回到主线程刷新表格数据 实现这个情境的方式有很多,但是现在以GCD的队列组来实现 步骤: 1.创建一个组 2.将下载图片的操作加入到组中 3.下载所有图片后,将下载图片的操作从组中移除 4.在notify中执行主队列操作 注意:当组中没有其它操作后,系统会主动调用no
iOS-GCD 队列组的使用
// 1.队列组 dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2.下载图片1 __blo...
多线程 GCD队列组
// DYFViewController.m // 623-08-队列组 // // Created by dyf on 14-6-23. // Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved. // #import "DYFViewController.h" @interfac...
GCD全解-dispatch-多个网络请求处理
1、问题 2、解决方案 2.1、一般方式:不做任何处理连续请求多个协议 2.2、GCD队列组:dispatch_group_t 2.3、GCD信号量:dispatch_semaphore_t 2.4、GCD信号量实现GCD队列组功能(不推荐)
队列组的使用
队列组可以监视队列组里面队列里面的任务执行情况 写法一 //创建一个并发队列 dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //创建一个队列组 dispatch_group_t group = dispatch_group_create(); //我们将所有进行多线程的任务放在队列组异步函数中(和普通的异步函数多了一个group...
GCD—队列组
#import "ViewController.h" //宏定义全局并发队列 #define global_quque dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //宏定义主队列 #define main_queue dispatch_get_main_queue() @interface Vi
OC 线程操作 - GCD队列组
1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT); ...
iOS GCD(二) dispatch_group 队列组
有时候我们会有这样的需求:分别异步执行2个耗时任务,然后当2个耗时任务都执行完毕后再回到主线程执行任务。这时候我们可以用到 GCD 的队列组。 使用场景: 同时上传多个图片,所有图片上传完成之后去请求服务器上传图片地址。 dispatch_group_notify 原理:使用函数dispatch_group_create创建dispatch group,然后使用函数dispatch_group_a...
GCD简介
什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 GCD的优势 GCD是苹果公司为多核的并行运算提出的解决方案 GCD会自动利用更多的CPU内核(比如双核、四核) GCD会自动管理线程的生命周期(创建线程、调度任务、销毁线程) 程序员只需要告诉GCD想要执行什么任务,不需...
2411



被折叠的 条评论
为什么被折叠?



