OStack程序员社区-中国程序员成长平台

标题: ios - 编码图像时调度异步卡在主线程上 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:09
标题: ios - 编码图像时调度异步卡在主线程上

所以我正在获取这个 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-推荐答案


试试这个代码:

- (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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4