UIImage&Base64流转换

96
eduzon
2018.07.10 17:43 字数 134

  UIImage转成base64串

UIImage *image = self.pickImageView.image;

NSData *imageData = UIImageJPEGRepresentation(image, 0.3f);

//NSDataBase64EncodingEndLineWithLineFeed

这个枚举值是base64串不换行

NSString *imageBase64Str = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

//    NSLog(@"%@",imageBase64Str);

if (imageBase64Str.length == 0) {

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选取图片" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alert addAction:okAction];

[self presentViewController:alert animated:NO completion:nil];

return;

}

  base64串转UIImage

NSData *imageData = [[NSData alloc] initWithBase64EncodedString:imageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];

UIImage *image = [UIImage imageWithData:imageData];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = [UIScreen mainScreen].bounds;

[self.view addSubview:imageView];

日记本
Web note ad 1