iOS图片转base64并上传服务器

1、图片image转base64


-(NSString *)imageChangeBase64: (UIImage *)image{

NSData  *imageData = nil;

//NSString *mimeType  = nil;

if ([self imageHasAlpha:image]) {

imageData = UIImagePNGRepresentation(image);

//mimeType = @"image/png";

}else{

imageData = UIImageJPEGRepresentation(image, 0.3f);

//mimeType = @"image/jpeg";

}

return [NSString stringWithFormat:@"%@",[imageData base64EncodedStringWithOptions: 0]];

}

-(BOOL)imageHasAlpha:(UIImage *)image{

CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);

return (alpha == kCGImageAlphaFirst ||

alpha == kCGImageAlphaLast ||

alpha == kCGImageAlphaPremultipliedFirst ||

alpha == kCGImageAlphaPremultipliedLast);

}



注意::上传的base64字符串 前加文件格式[NSString stringWithFormat:@"png@%@",strImage];

你可能感兴趣的:(iOS图片转base64并上传服务器)