ios - 编码图像时调度异步卡在主线程上
<p><p>所以我正在获取这个 UIImage 数据并将其转换为 base64 中的字符串。问题是它在转换时卡在 UI 线程上,我不知道为什么。 </p>
<pre><code>- (void)processImage:(UIImage*)image{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
;
;
dispatch_async(myQueue, ^{
// Convert image
NSData *myData = ;
NSString *myString = ];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
;
snapShotInBase64 = myString;
;
});
});
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个代码:</p>
<pre><code>- (void)processImage:(UIImage*)image{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
;
;
dispatch_async(myQueue, ^{
// Convert image
NSData *myData = ;
NSString *myString = ];
snapShotInBase64 = myString;
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
;
});
});
dispatch_barrier_async(myQueue, ^{
;
});
}
</code></pre>
<p>或</p>
<pre><code>- (void)processImage:(UIImage*)image{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
;
;
dispatch_async(myQueue, ^{
// Convert image
NSData *myData = ;
NSString *myString = ];
snapShotInBase64 = myString;
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
;
dispatch_async(myQueue, ^{
;
});
});
});
}
</code></pre>
<p>希望会有所帮助。如果你在服务器上传图片,为什么不使用 <a href="https://github.com/AFNetworking/AFNetworking" rel="noreferrer noopener nofollow">AFNetworking</a>图书馆</p></p>
<p style="font-size: 20px;">关于ios - 编码图像时调度异步卡在主线程上,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19794936/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19794936/
</a>
</p>
页:
[1]