UIImage数据返回null

问题描述 投票:0回答:2

我试图将UIImage转换为NSData,然后将NSData转换为Base64字符串。

这是我处理图像的方法:

- (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, ^{
        NSData *myData = nil;
        myData = UIImagePNGRepresentation(image);

        float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (sysVer >= 7.00) {
            // Convert image
            myData = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        }

        NSString *myString = [NSString stringWithUTF8String:[myData bytes]];
        snapShotInBase64 = myString;

        NSLog(@"Snapshot in base64 string: %@", myString);

        dispatch_async(dispatch_get_main_queue(), ^{
            // BACK TO THE UI THREAD
            [self showSuccessAlertView:@"Great" message:@"Sending Image..."];

            isProcessingImage = NO;
            [self sendImagePostRequest];
        });
    });
}

问题是NSLog回来了!

NSLog(@"Snapshot in base64 string: %@", myString);
ios uiimage base64
2个回答
0
投票

我最终使用了Base64 NSData扩展类。

https://github.com/nicklockwood/Base64

您可以直接使用他的Base64.h和.m文件,然后您可以调用一种方法将您的数据转换为base64。这很神奇。


0
投票

你能试试这个吗?我认为这对你很有帮助。

图片到base64: -


if let updatedImage = originalImaglable.updateImageOrientionUpSide() {

        let imageData: NSData? = UIImageJPEGRepresentation(updatedImage, 0.1) as NSData?

        let dataImage = imageData?.base64EncodedString(options: .lineLength64Characters)

        if let strdata = dataImage {

            str_photo = strdata
        }
  }
© www.soinside.com 2019 - 2024. All rights reserved.