我正在尝试将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返回null!
NSLog(@"Snapshot in base64 string: %@", myString);
答案 0 :(得分:0)
我最终使用了Base64 NSData扩展类。
https://github.com/nicklockwood/Base64
你可以直接使用他的Base64.h和.m文件,然后你可以调用一种方法将数据转换为base64。这很神奇。
答案 1 :(得分: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
}
}