所以我正在获取这个 UIImage 数据并将其转换为 base64 中的字符串。问题是它在转换时卡在 UI 线程上,我不知道为什么。
- (void)processImageUIImage*)image{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
[self.spinnerOutlet setAlpha:0.0f];
[self.spinnerOutlet startAnimating];
dispatch_async(myQueue, ^{
// Convert image
NSData *myData = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *myString = [NSString stringWithUTF8String:[myData bytes]];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
[self showSuccessAlertView"Success!" message"Submitting Image..."];
snapShotInBase64 = myString;
[self sendImagePostRequest];
});
});
}
Best Answer-推荐答案 strong>
试试这个代码:
- (void)processImageUIImage*)image{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
[self.spinnerOutlet setAlpha:0.0f];
[self.spinnerOutlet startAnimating];
dispatch_async(myQueue, ^{
// Convert image
NSData *myData = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *myString = [NSString stringWithUTF8String:[myData bytes]];
snapShotInBase64 = myString;
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
[self showSuccessAlertView"Success!" message"Submitting Image..."];
});
});
dispatch_barrier_async(myQueue, ^{
[self sendImagePostRequest];
});
}
或
- (void)processImageUIImage*)image{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
[self.spinnerOutlet setAlpha:0.0f];
[self.spinnerOutlet startAnimating];
dispatch_async(myQueue, ^{
// Convert image
NSData *myData = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *myString = [NSString stringWithUTF8String:[myData bytes]];
snapShotInBase64 = myString;
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
[self showSuccessAlertView"Success!" message"Submitting Image..."];
dispatch_async(myQueue, ^{
[self sendImagePostRequest];
});
});
});
}
希望会有所帮助。如果你在服务器上传图片,为什么不使用 AFNetworking图书馆
关于ios - 编码图像时调度异步卡在主线程上,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/19794936/
|