2

所以我正在获取这个 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];
        });

    });



}
4

1 回答 1

1

试试这个代码:

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

于 2013-11-05T17:53:05.923 回答