iOS image转base64

- (NSString *) convertImage: (UIImage *) image
{
    NSData *imageData = nil;
    NSString *mimeType = nil;
    
    if ([self imageHasAlpha: image]) {
        imageData = UIImagePNGRepresentation(image);
        mimeType =  pngType;
    } else {
        imageData = UIImageJPEGRepresentation(image, 1.0f);
        mimeType = jpgType;
    }
    
    return [NSString stringWithFormat: base64, mimeType,
            [imageData base64EncodedStringWithOptions: 0]];
    
}

- (BOOL) imageHasAlpha: (UIImage *) image
{
    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
    return (alpha == kCGImageAlphaFirst ||
            alpha == kCGImageAlphaLast ||
            alpha == kCGImageAlphaPremultipliedFirst ||
            alpha == kCGImageAlphaPremultipliedLast);
}
  • tip 服务端接收数据是, android和ios可能转换的不一样, 服务端如果不做处理需要, 此处代码是weex代码, js分离获取base64字符串


    iOS image转base64_第1张图片
    image.png

你可能感兴趣的:(iOS image转base64)