{"id":288,"date":"2023-05-10T13:25:12","date_gmt":"2023-05-10T05:25:12","guid":{"rendered":"https:\/\/www.aqrboyblog.top\/?p=288"},"modified":"2023-05-15T13:27:18","modified_gmt":"2023-05-15T05:27:18","slug":"ios-%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.aqrboyblog.top\/?p=288","title":{"rendered":"iOS \u591a\u7ebf\u7a0b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001iOS\u4e2d\u5e38\u89c1\u7684\u591a\u7ebf\u7a0b\u65b9\u6848<\/h2>\n\n\n\n<h5 class=\"wp-block-heading\">1\u3001pthread<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u5957\u901a\u7528\u7684\u591a\u7ebf\u7a0bAPI<\/li>\n\n\n\n<li>\u9002\u7528\u4e8eUnix\\Linux\\Windows\u7b49\u7cfb\u7edf<\/li>\n\n\n\n<li>\u8de8\u5e73\u53f0\u3001\u53ef\u79fb\u690d<\/li>\n\n\n\n<li>\u4f7f\u7528\u96be\u5ea6\u5927<\/li>\n\n\n\n<li>\u4f7f\u7528C\u8bed\u8a00<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u5458\u7ba1\u7406\u7ebf\u7a0b\u751f\u547d\u5468\u671f<\/li>\n\n\n\n<li>\u5b9e\u9645\u9879\u76ee\u4e2d\u51e0\u4e4e\u4e0d\u7528<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">2\u3001NSThread<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u66f4\u52a0\u9762\u5411\u5bf9\u8c61<\/li>\n\n\n\n<li>\u7b80\u5355\u6613\u7528\u3001\u53ef\u76f4\u63a5\u64cd\u4f5c\u7ebf\u7a0b\u5bf9\u8c61<\/li>\n\n\n\n<li>\u4f7f\u7528OC\u8bed\u8a00<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u5458\u7ba1\u7406\u7ebf\u7a0b\u751f\u547d\u5468\u671f<\/li>\n\n\n\n<li>\u5b9e\u9645\u9879\u76ee\u4e2d\u51e0\u4e4e\u4e0d\u7528<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">3\u3001GCD<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e8\u5728\u66ff\u4ee3NSThread\u6280\u672f<\/li>\n\n\n\n<li>\u5145\u5206\u5229\u7528\u8bbe\u5907\u7684\u591a\u6838<\/li>\n\n\n\n<li>\u4f7f\u7528C\u8bed\u8a00<\/li>\n\n\n\n<li>\u81ea\u52a8\u7ba1\u7406\u7ebf\u7a0b\u751f\u547d\u5468\u671f<\/li>\n\n\n\n<li>\u5b9e\u9645\u9879\u76ee\u4e2d\u7ecf\u5e38\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">4\u3001NSOperation<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u57fa\u4e8eGCD\uff08\u5e95\u5c42\u662fGCD\uff09<\/li>\n\n\n\n<li>\u6bd4GCD\u591a\u4e86\u4e00\u4e9b\u7b80\u5355\u5b9e\u7528\u7684\u529f\u80fd<\/li>\n\n\n\n<li>\u4f7f\u7528\u66f4\u52a0\u9762\u5411\u5bf9\u8c61<\/li>\n\n\n\n<li>\u4f7f\u7528OC\u8bed\u8a00<\/li>\n\n\n\n<li>\u81ea\u52a8\u7ba1\u7406\u7ebf\u7a0b\u751f\u547d\u5468\u671f<\/li>\n\n\n\n<li>\u5b9e\u9645\u9879\u76ee\u4e2d\u7ecf\u5e38\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001GCD<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">1\u3001GCD\u6709\u4e24\u4e2a\u7528\u6765\u6267\u884c\u4efb\u52a1\u7684\u51fd\u6570<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u540c\u6b65\uff08sync\uff09\uff1a\u53ea\u80fd\u5728\u5f53\u524d\u7ebf\u7a0b\u4e2d\u6267\u884c\u4efb\u52a1\uff0c\u4e0d\u5177\u5907\u5f00\u542f\u65b0\u7ebf\u7a0b\u7684\u80fd\u529b\uff0c\u4efb\u52a1\u7acb\u523b\u9a6c\u4e0a\u6267\u884c\uff0c\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u5e76\u7b49\u5f85 Block\u4e2d\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5dispatch\u51fd\u6570\u624d\u4f1a\u8fd4\u56de\uff0c\u7136\u540e\u5f53\u524d\u7ebf\u7a0b\u624d\u4f1a\u7ee7\u7eed\u5f80\u4e0b\u8fd0\u884c\u3002<\/li>\n\n\n\n<li>\u5f02\u6b65\uff08async\uff09\uff1a\u53ef\u4ee5\u5728\u65b0\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u4efb\u52a1\uff0c\u5177\u5907\u5f00\u542f\u7ebf\u7a0b\u7684\u80fd\u529b\uff0c\u4f46\u4e0d\u4e00\u5b9a\u4f1a\u5f00\u542f\u65b0\u7684\u7ebf\u7a0b\uff0cdispatch\u51fd\u6570\u4f1a\u7acb\u5373\u8fd4\u56de, \u7136\u540eBlock\u5728\u540e\u53f0\u5f02\u6b65\u6267\u884c\uff0c\u5373\u5f53\u524d\u7ebf\u7a0b\u4f1a\u76f4\u63a5\u5f80\u4e0b\u6267\u884c\uff0c\u4e0d\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#pragma mark - \u540c\u6b65\u6267\u884c\n- (void)syncQueue {\n    NSLog(@\"\u540c\u6b65\u4e3b\u7ebf\u7a0b\u5f00\u59cb\");\n    \/\/\u521b\u5efa\u4e32\u884c\u961f\u5217\n    dispatch_queue_t queue = dispatch_queue_create(\"com.weixin.globalQueue\", DISPATCH_QUEUE_SERIAL);\n    dispatch_sync(queue, ^{\n        NSLog(@\"\u540c\u6b65\u7ebf\u7a0b\");\n    });\n    NSLog(@\"\u540c\u6b65\u4e3b\u7ebf\u7a0b\u7ed3\u675f\");\n}\n\n#pragma mark - \u5f02\u6b65\u6267\u884c\n- (void)asyncQueue {\n    NSLog(@\"\u5f02\u6b65\u4e3b\u7ebf\u7a0b\u5f00\u59cb\");\n    dispatch_queue_t queue = dispatch_queue_create(\"com.qq.globalQueue\", DISPATCH_QUEUE_SERIAL);\n    dispatch_async(queue, ^{\n        NSLog(@\"\u5f02\u6b65\u7ebf\u7a0b\");\n    });\n    NSLog(@\"\u5f02\u6b65\u4e3b\u7ebf\u7a0b\u7ed3\u675f\");\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2\u3001\u961f\u5217<\/h4>\n\n\n\n<p>\u7528\u4e8e\u5b58\u653e\u4efb\u52a1\uff0c\u5206\u4e3a\u4e32\u884c\u961f\u5217\u548c\u5e76\u884c\u961f\u5217\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e32\u884c\u961f\u5217\uff1a\u6240\u6709\u4efb\u52a1\u4f1a\u5728\u4e00\u6761\u7ebf\u7a0b\u4e2d\u6267\u884c\uff08\u6709\u53ef\u80fd\u662f\u5f53\u524d\u7ebf\u7a0b\u4e5f\u6709\u53ef\u80fd\u662f\u65b0\u5f00\u8f9f\u7684\u7ebf\u7a0b\uff09\uff0c\u5e76\u4e14\u4e00\u4e2a\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u624d\u5f00\u59cb\u6267\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u5e76\u884c\u961f\u5217\uff1a\u53ef\u4ee5\u5f00\u542f\u591a\u6761\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u4efb\u52a1\uff08\u4f46\u4e0d\u4e00\u5b9a\u4f1a\u5f00\u542f\u65b0\u7684\u7ebf\u7a0b\uff09\uff0c\u5e76\u4e14\u5f53\u4e00\u4e2a\u4efb\u52a1\u653e\u5230\u6307\u5b9a\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u65f6\uff0c\u4e0b\u4e00\u4e2a\u4efb\u52a1\u5c31\u53ef\u4ee5\u5f00\u59cb\u6267\u884c\u4e86<\/li>\n\n\n\n<li>\u521b\u5efa\u961f\u5217\uff1a<\/li>\n<\/ul>\n\n\n\n<p>1.\u4e32\u884c\u961f\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u521b\u5efa\u4e32\u884c\u961f\u5217\ndispatch_queue_t firstQueue = dispatch_queue_create(\"com.weibo\", DISPATCH_QUEUE_SERIAL);<\/code><\/pre>\n\n\n\n<p>2.\u5e76\u884c\u961f\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u521b\u5efa\u5e76\u884c\u961f\u5217\ndispatch_queue_t secondQueue = dispatch_queue_create(\"com.facebook\", DISPATCH_QUEUE_CONCURRENT);<\/code><\/pre>\n\n\n\n<p>3.\u521b\u5efa\u5168\u5c40\u9ed8\u8ba4\u5e76\u53d1\u961f\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u521b\u5efa\u5168\u5c40\u9ed8\u8ba4\u5e76\u53d1\u961f\u5217\n\/**\n   \u7b2c\u4e00\u4e2a\u53c2\u6570:\u4f18\u5148\u7ea7 \u4e5f\u53ef\u76f4\u63a5\u586b\u540e\u9762\u7684\u6570\u5b57\n   #define DISPATCH_QUEUE_PRIORITY_HIGH 2 \/\/ \u9ad8\n   #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 \/\/ \u9ed8\n   #define DISPATCH_QUEUE_PRIORITY_LOW (-2) \/\/ \u4f4e\n   #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN \/\/ \u540e\u53f0\n   \u7b2c\u4e8c\u4e2a\u53c2\u6570: \u9884\u7559\u53c2\u6570  0\n*\/\ndispatch_queue_t queue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);<\/code><\/pre>\n\n\n\n<p>4.\u83b7\u53d6\u4e3b\u961f\u5217<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u83b7\u53d6\u4e3b\u961f\u5217\ndispatch_queue_t mainQueue = dispatch_get_main_queue();<\/code><\/pre>\n\n\n\n<p>5.\u961f\u5217\u7ec4<\/p>\n\n\n\n<p><span style=\"color: rgb(64, 64, 64); font-family: -apple-system, &quot;system-ui&quot;, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Segoe UI&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\">dispatch_group_async &amp; dispatch_group_notify<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#pragma mark - \u961f\u5217\u7ec4\n- (void)GCDGroup {\n    \/\/\u521b\u5efa\u961f\u5217\u7ec4\n    dispatch_group_t group = dispatch_group_create();\n    \/\/1.\u5f00\u5b50\u7ebf\u7a0b\u4e0b\u8f7d\u56fe\u7247\n    \/\/\u521b\u5efa\u961f\u5217(\u5e76\u53d1)\n    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);\n    \n    \/\/\u4e0b\u8f7d\u56fe\u72471\n    dispatch_group_async(group, queue, ^{\n        NSURL *url = &#91;NSURL URLWithString:@\"http:\/\/www.huabian.com\/uploadfile\/2015\/0914\/20150914014032274.jpg\"];\n        NSData *data = &#91;NSData dataWithContentsOfURL:url];\n        self.image1 = &#91;UIImage imageWithData:data];\n        NSLog(@\"image1 ==== %@\",self.image1);\n    });\n    \n    \/\/\u4e0b\u8f7d\u56fe\u72472\n    dispatch_group_async(group, queue, ^{\n        NSURL *url = &#91;NSURL URLWithString:@\"http:\/\/img1.3lian.com\/img2011\/w12\/1202\/19\/d\/88.jpg\"];\n        NSData *data = &#91;NSData dataWithContentsOfURL:url];\n        self.image2 = &#91;UIImage imageWithData:data];\n        NSLog(@\"image2 ==== %@\",self.image2);\n    });\n    \n    \/\/group\u4e2d\u6240\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u901a\u77e5\u8be5\u65b9\u6cd5\u6267\u884c\n    dispatch_group_notify(group, queue, ^{\n        \/\/\u5f00\u542f\u56fe\u5f62\u4e0a\u4e0b\u6587\n        UIGraphicsBeginImageContext(CGSizeMake(200, 200));\n        \/\/\u753b1\n        &#91;self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];\n        \/\/\u753b2\n        &#91;self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];\n        \/\/\u6839\u636e\u56fe\u5f62\u4e0a\u4e0b\u6587\u62ff\u5230\u56fe\u7247\n        UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();\n        \/\/\u5173\u95ed\u4e0a\u4e0b\u6587\n        UIGraphicsEndImageContext();\n        \/\/\u56de\u5230\u4e3b\u7ebf\u7a0b\u5237\u65b0UI\n        dispatch_async(dispatch_get_main_queue(), ^{\n            self.imageView.image = image;\n            NSLog(@\"%@--\u5237\u65b0UI\",&#91;NSThread currentThread]);\n        });\n    });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p class=\"text-justify\">\u4e00\u3001iOS\u4e2d\u5e38\u89c1\u7684\u591a\u7ebf\u7a0b\u65b9\u6848 1\u3001pthre [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=288"}],"version-history":[{"count":1,"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqrboyblog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}