gpt4 book ai didi

ios - 分派(dispatch)异步在编码图像时卡在主线程上

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:08 29 4
gpt4 key购买 nike

所以我正在获取此 UIImage 数据并将其转换为 base64 中的字符串。问题是它在转换时卡在 UI 线程上,我不确定为什么。

- (void)processImage:(UIImage*)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];
});

});



}

最佳答案

试试这个代码:

- (void)processImage:(UIImage*)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)processImage:(UIImage*)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 - 分派(dispatch)异步在编码图像时卡在主线程上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19794936/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com